Skip to content
Merged
Show file tree
Hide file tree
Changes from 93 commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
e04d7cd
tsa
Dec 6, 2023
b90399c
resolved conflicts
Jan 4, 2024
f249e7a
update
Jan 4, 2024
7d888dc
Merge branch 'notaryproject:main' into tsa
Jan 15, 2024
3cbd3e5
update
Jan 16, 2024
9edc3c0
resolve conflicts
Jan 16, 2024
d2a86e4
resolved conflicts
Jan 23, 2024
95aad90
Merge branch 'notaryproject:main' into tsa
Jan 24, 2024
68036b6
resolved conflicts
Jan 25, 2024
1622205
Merge branch 'notaryproject:main' into tsa
Jan 25, 2024
f14a98b
Merge branch 'notaryproject:main' into tsa
Jan 31, 2024
e84f407
update tspclient-go
Feb 1, 2024
0e83d2f
tsa
Feb 1, 2024
a45c5dc
update
Feb 1, 2024
d7d7a1a
resolved conflicts
Feb 2, 2024
e73312f
resolved conflicts
Mar 18, 2024
c15b58d
Merge branch 'notaryproject:main' into tsa
Mar 22, 2024
a851775
update
Mar 22, 2024
0bd76bc
Merge branch 'notaryproject:main' into tsa
Mar 27, 2024
eb0fcc9
updated timestamp
Mar 27, 2024
7f60e9e
Merge branch 'notaryproject:main' into tsa
Mar 28, 2024
f7bd27c
Merge branch 'notaryproject:main' into tsa
Apr 8, 2024
be15eb2
test
Apr 8, 2024
d4fa037
Merge branch 'notaryproject:main' into tsa
Apr 10, 2024
177be5b
update
Apr 10, 2024
64ffee5
added at-timestamped-time in verification
Apr 11, 2024
b0c8b45
added cli spec
Apr 12, 2024
d9ae5aa
resolved conflicts
Apr 16, 2024
47872ad
update
Apr 18, 2024
f3a96c0
Merge branch 'notaryproject:main' into tsa
Apr 18, 2024
5c7a70b
test
Apr 18, 2024
7aee992
fixed e2e tests
Apr 18, 2024
991b5d4
Merge branch 'notaryproject:main' into tsa
Apr 25, 2024
4b3a06d
updated per spec
May 8, 2024
4cb6be6
fixed e2e tests
May 8, 2024
b498611
updated tspclient-go
May 10, 2024
1456f7a
resolved conflicts
Jun 4, 2024
7ffa82b
timestamping
Jun 4, 2024
ca20476
fixed e2e tests
Jun 4, 2024
9e60d23
resolved conflicts
Jun 11, 2024
9dca4dc
update
Jun 11, 2024
61107e5
Merge branch 'notaryproject:main' into tsa
Jun 18, 2024
6cd87c8
updated timestamping
Jun 18, 2024
0040af6
update
Jun 20, 2024
a860d8b
updated timestamp
Jun 21, 2024
9ecbb9a
adding tests
Jun 24, 2024
ad1fb95
fix tests
Jun 24, 2024
e148bba
fix tests
Jun 24, 2024
d7e5186
added more e2e tests
Jun 24, 2024
388d223
fix e2e
Jun 24, 2024
e34897c
fix e2e
Jun 24, 2024
c7debce
fix e2e
Jun 24, 2024
c8c4a09
added tsa-root-cert
Jun 24, 2024
7ef3850
updated e2e tests
Jun 25, 2024
03f3303
fixed e2e tests
Jun 25, 2024
8e0f32c
update
Jun 25, 2024
480bd4b
updated CLI spec for timestamping
Jun 25, 2024
7cd46ab
update
Jun 26, 2024
92b5ce9
update
Jun 27, 2024
f9ca829
test
Jun 27, 2024
1a8fe7b
test
Jun 27, 2024
1526dbe
add more tests
Jun 27, 2024
f78ee81
fix e2e
Jun 27, 2024
670e39d
updated tspclient-go
Jun 27, 2024
a11b692
updated timestamping
Jul 1, 2024
10a2b63
fixed E2E test
Jul 1, 2024
bd7212d
updated timestamping
Jul 3, 2024
b241460
updated timestamping
Jul 3, 2024
8900872
updated timestamping
Jul 3, 2024
bdf1264
updated notation-go
Jul 8, 2024
04579ca
updated notation-go
Jul 9, 2024
52f7052
updated notation-go
Jul 11, 2024
1e4422d
update
Jul 11, 2024
34e2f1f
updated notation-go
Jul 11, 2024
ec602c0
updated notation-go
Jul 12, 2024
e4564a3
fixed e2e
Jul 12, 2024
469c37a
resolved conflicts
Jul 12, 2024
f5a4ad9
updated notation-go
Jul 12, 2024
aa35f19
updated dependencies
Jul 15, 2024
1a82b19
updated dependencies
Jul 15, 2024
abcc089
updated verify spec
Jul 15, 2024
940a728
updated verify spec
Jul 15, 2024
6332224
updated specs
Jul 15, 2024
7959952
updated per code review
Jul 15, 2024
efa5a28
updated per code review
Jul 16, 2024
a95b987
update
Jul 16, 2024
76ee7c7
fix
Jul 16, 2024
4364750
update dependencies
Jul 16, 2024
ebebefe
update
Jul 16, 2024
41b5551
naming
Jul 17, 2024
81f01ba
updated per code review
Jul 17, 2024
f8c2d8e
Merge branch 'notaryproject:main' into tsa
Jul 17, 2024
1b97540
update
Jul 17, 2024
49c8b81
updated per code review
Jul 22, 2024
5c177c0
fix E2E test
Jul 22, 2024
7c44b3e
update
Jul 22, 2024
6aafe34
update
Jul 22, 2024
26bc1d3
updated E2E tests
Jul 22, 2024
7b996ec
fixed E2E tests
Jul 22, 2024
7aef357
fixed E2E tests
Jul 22, 2024
3196711
fixed E2E tests
Jul 22, 2024
cafc2e1
updated dependencies
Jul 22, 2024
b05572b
updated per code review
Jul 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/notation/cert/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ Example - Add a certificate to the "ca" type of a named store "acme-rockets":

Example - Add a certificate to the "signingAuthority" type of a named store "wabbit-networks":
notation cert add --type signingAuthority --store wabbit-networks wabbit-networks.pem

Example - Add a certificate to the "tsa" type of a named store "timestamp":
notation cert add --type tsa --store timestamp wabbit-networks-timestamp.pem
`,
RunE: func(cmd *cobra.Command, args []string) error {
return addCerts(opts)
Expand Down
3 changes: 3 additions & 0 deletions cmd/notation/cert/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ Example - Delete certificate "cert1.pem" with "signingAuthority" type from trust

Example - Delete all certificates with "ca" type from the trust store "acme-rockets", without prompt for confirmation:
notation cert delete --type ca --store acme-rockets -y --all

Example - Delete certificate "wabbit-networks-timestamp.pem" with "tsa" type from trust store timestamp:
notation cert delete --type tsa --store timestamp wabbit-networks-timestamp.pem -y
`,
RunE: func(cmd *cobra.Command, args []string) error {
return deleteCerts(opts)
Expand Down
3 changes: 3 additions & 0 deletions cmd/notation/cert/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Example - List all certificate files from trust store of type "ca"

Example - List all certificate files from trust store "wabbit-networks" of type "signingAuthority"
notation cert ls --type signingAuthority --store "wabbit-networks"

Example - List all certificate files from trust store of type "tsa"
notation cert ls --type tsa
`,
RunE: func(cmd *cobra.Command, args []string) error {
return listCerts(cmd.Context(), opts)
Expand Down
3 changes: 3 additions & 0 deletions cmd/notation/cert/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ Example - Show details of certificate "cert1.pem" with type "ca" from trust stor

Example - Show details of certificate "cert2.pem" with type "signingAuthority" from trust store "wabbit-networks":
notation cert show --type signingAuthority --store wabbit-networks cert2.pem

Example - Show details of certificate "wabbit-networks-timestamp.pem" with type "tsa" from trust store "timestamp":
notation cert show --type tsa --store timestamp wabbit-networks-timestamp.pem
`,
RunE: func(cmd *cobra.Command, args []string) error {
return showCerts(cmd.Context(), opts)
Expand Down
67 changes: 57 additions & 10 deletions cmd/notation/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,46 @@
package main

import (
"crypto/x509"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"

corex509 "github.com/notaryproject/notation-core-go/x509"
"github.com/notaryproject/notation-go"
"github.com/notaryproject/notation/cmd/notation/internal/experimental"
"github.com/notaryproject/notation/internal/cmd"
"github.com/notaryproject/notation/internal/envelope"
"github.com/notaryproject/notation/internal/httputil"
"github.com/notaryproject/tspclient-go"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)

const referrersTagSchemaDeleteError = "failed to delete dangling referrers index"

// timestampingTimeout is the timeout when requesting timestamp countersignature
// from a TSA
const timestampingTimeout = 15 * time.Second

type signOpts struct {
cmd.LoggingFlagOpts
cmd.SignerFlagOpts
SecureFlagOpts
expiry time.Duration
pluginConfig []string
userMetadata []string
reference string
allowReferrersAPI bool
forceReferrersTag bool
ociLayout bool
inputType inputType
expiry time.Duration
pluginConfig []string
userMetadata []string
reference string
allowReferrersAPI bool
forceReferrersTag bool
ociLayout bool
inputType inputType
tsaServerURL string
tsaRootCertificatePath string
}

func signCommand(opts *signOpts) *cobra.Command {
Expand Down Expand Up @@ -74,6 +86,9 @@ Example - Sign an OCI artifact stored in a registry and specify the signature ex

Example - Sign an OCI artifact and store signature using the Referrers API. If it's not supported, fallback to the Referrers tag schema
notation sign --force-referrers-tag=false <registry>/<repository>@<digest>

Example - Sign an OCI artifact with timestamping:
notation sign --timestamp-url <TSA_url> --timestamp-root-cert <TSA_root_certificate_filepath> <registry>/<repository>@<digest>
`
experimentalExamples := `
Example - [Experimental] Sign an OCI artifact referenced in an OCI layout
Expand Down Expand Up @@ -101,6 +116,16 @@ Example - [Experimental] Sign an OCI artifact identified by a tag and referenced
return experimental.CheckFlagsAndWarn(cmd, "allow-referrers-api", "oci-layout")
},
RunE: func(cmd *cobra.Command, args []string) error {
// timestamping
if cmd.Flags().Changed("timestamp-url") {
if opts.tsaServerURL == "" {
return errors.New("timestamping: tsa url cannot be empty")
}
if opts.tsaRootCertificatePath == "" {
return errors.New("timestamping: tsa root certificate path cannot be empty")
}
}

// allow-referrers-api flag is set
if cmd.Flags().Changed("allow-referrers-api") {
if opts.allowReferrersAPI {
Expand All @@ -120,9 +145,12 @@ Example - [Experimental] Sign an OCI artifact identified by a tag and referenced
cmd.SetPflagPluginConfig(command.Flags(), &opts.pluginConfig)
cmd.SetPflagUserMetadata(command.Flags(), &opts.userMetadata, cmd.PflagUserMetadataSignUsage)
cmd.SetPflagReferrersAPI(command.Flags(), &opts.allowReferrersAPI, fmt.Sprintf(cmd.PflagReferrersUsageFormat, "sign"))
command.Flags().StringVar(&opts.tsaServerURL, "timestamp-url", "", "RFC 3161 Timestamping Authority (TSA) server URL")
command.Flags().StringVar(&opts.tsaRootCertificatePath, "timestamp-root-cert", "", "filepath of timestamp authority root certificate")
cmd.SetPflagReferrersTag(command.Flags(), &opts.forceReferrersTag, "force to store signatures using the referrers tag schema")
command.Flags().BoolVar(&opts.ociLayout, "oci-layout", false, "[Experimental] sign the artifact stored as OCI image layout")
command.MarkFlagsMutuallyExclusive("oci-layout", "force-referrers-tag", "allow-referrers-api")
command.MarkFlagsRequiredTogether("timestamp-url", "timestamp-root-cert")
experimental.HideFlags(command, experimentalExamples, []string{"oci-layout"})
return command
}
Expand All @@ -140,7 +168,7 @@ func runSign(command *cobra.Command, cmdOpts *signOpts) error {
if err != nil {
return err
}
signOpts, err := prepareSigningOpts(cmdOpts)
signOpts, err := prepareSigningOpts(ctx, cmdOpts)
if err != nil {
return err
}
Expand Down Expand Up @@ -168,7 +196,7 @@ func runSign(command *cobra.Command, cmdOpts *signOpts) error {
return nil
}

func prepareSigningOpts(opts *signOpts) (notation.SignOptions, error) {
func prepareSigningOpts(ctx context.Context, opts *signOpts) (notation.SignOptions, error) {
mediaType, err := envelope.GetEnvelopeMediaType(opts.SignerFlagOpts.SignatureFormat)
if err != nil {
return notation.SignOptions{}, err
Expand All @@ -189,5 +217,24 @@ func prepareSigningOpts(opts *signOpts) (notation.SignOptions, error) {
},
UserMetadata: userMetadata,
}
if opts.tsaServerURL != "" {
// timestamping
fmt.Printf("Configured to timestamp with TSA %q\n", opts.tsaServerURL)
Comment thread
This conversation was marked as resolved.
signOpts.Timestamper, err = tspclient.NewHTTPTimestamper(httputil.NewClient(ctx, &http.Client{Timeout: timestampingTimeout}), opts.tsaServerURL)
if err != nil {
return notation.SignOptions{}, fmt.Errorf("cannot get http timestamper for timestamping: %w", err)
}

rootCerts, err := corex509.ReadCertificateFile(opts.tsaRootCertificatePath)
if err != nil {
return notation.SignOptions{}, err
}
if len(rootCerts) == 0 {
return notation.SignOptions{}, fmt.Errorf("cannot find any tsa root certificate from %q. Expecting x509 certificate in PEM or DER format from the file", opts.tsaRootCertificatePath)
Comment thread
This conversation was marked as resolved.
Outdated
Comment thread
This conversation was marked as resolved.
Outdated
}
rootCAs := x509.NewCertPool()
rootCAs.AddCert(rootCerts[0])
Comment thread
This conversation was marked as resolved.
Outdated
signOpts.TSARootCAs = rootCAs
}
return signOpts, nil
}
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ module github.com/notaryproject/notation
go 1.22

require (
github.com/notaryproject/notation-core-go v1.0.3
github.com/notaryproject/notation-go v1.1.1
github.com/notaryproject/notation-core-go v1.0.4-0.20240716001320-f45197cbd53b
github.com/notaryproject/notation-go v1.1.1-0.20240715044011-b52583166f2b
github.com/notaryproject/tspclient-go v0.1.1-0.20240715235637-df25ef8d2172
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
golang.org/x/net v0.22.0
golang.org/x/term v0.22.0
oras.land/oras-go/v2 v2.5.0
)

require (
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
github.com/fxamacker/cbor/v2 v2.6.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-asn1-ber/asn1-ber v1.5.5 // indirect
github.com/go-ldap/ldap/v3 v3.4.8 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
Expand All @@ -25,8 +27,8 @@ require (
github.com/notaryproject/notation-plugin-framework-go v1.0.0 // indirect
github.com/veraison/go-cose v1.1.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.22.0 // indirect
)
22 changes: 12 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fxamacker/cbor/v2 v2.6.0 h1:sU6J2usfADwWlYDAFhZBQ6TnLFBHxgesMrQfQgk1tWA=
github.com/fxamacker/cbor/v2 v2.6.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=
github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
github.com/go-asn1-ber/asn1-ber v1.5.5 h1:MNHlNMBDgEKD4TcKr36vQN68BA00aDfjIt3/bD50WnA=
github.com/go-asn1-ber/asn1-ber v1.5.5/go.mod h1:hEBeB/ic+5LoWskz+yKT7vGhhPYkProFKoKdwZRWMe0=
github.com/go-ldap/ldap/v3 v3.4.8 h1:loKJyspcRezt2Q3ZRMq2p/0v8iOurlmeXDPw6fikSvQ=
Expand Down Expand Up @@ -35,12 +35,14 @@ github.com/jcmturner/gokrb5/v8 v8.4.4 h1:x1Sv4HaTpepFkXbt2IkL29DXRf8sOfZXo8eRKh6
github.com/jcmturner/gokrb5/v8 v8.4.4/go.mod h1:1btQEpgT6k+unzCwX1KdWMEwPPkkgBtP+F6aCACiMrs=
github.com/jcmturner/rpc/v2 v2.0.3 h1:7FXXj8Ti1IaVFpSAziCZWNzbNuZmnvw/i6CqLNdWfZY=
github.com/jcmturner/rpc/v2 v2.0.3/go.mod h1:VUJYCIDm3PVOEHw8sgt091/20OJjskO/YJki3ELg/Hc=
github.com/notaryproject/notation-core-go v1.0.3 h1:FCgvULSypEFrrNgvDRdHbKAGAgbXK43n/jKD9q2WECA=
github.com/notaryproject/notation-core-go v1.0.3/go.mod h1:eDo5/LTUp23mB7w0CckJLnl+p93oGdyiKDzzggpqTH4=
github.com/notaryproject/notation-go v1.1.1 h1:EAY8ERBWhrdaG9MIumSZ9xyUHktgr6OkCByd75HR+FA=
github.com/notaryproject/notation-go v1.1.1/go.mod h1:XykI2i5jHb6cGf+bcG/cIeNfNO2u4Xoy2mkuOKHjVVI=
github.com/notaryproject/notation-core-go v1.0.4-0.20240716001320-f45197cbd53b h1:uJ4bmNieZRkPj3UgmKr3bZr8vs7UJ2MdlJMeB0oOaZw=
github.com/notaryproject/notation-core-go v1.0.4-0.20240716001320-f45197cbd53b/go.mod h1:MdxSbL9F5h63EmtXWfYMWy7hEmGmOmsfN4B6KM2WyhY=
github.com/notaryproject/notation-go v1.1.1-0.20240715044011-b52583166f2b h1:Bz/b2CxF5zs4/+/o37zC47U8yipMBkFdP5QTZtqZfJc=
github.com/notaryproject/notation-go v1.1.1-0.20240715044011-b52583166f2b/go.mod h1:h0U0bVTjCxnozj1OhyeqQsNWWd7frFK+DUJsnH6tAhI=
github.com/notaryproject/notation-plugin-framework-go v1.0.0 h1:6Qzr7DGXoCgXEQN+1gTZWuJAZvxh3p8Lryjn5FaLzi4=
github.com/notaryproject/notation-plugin-framework-go v1.0.0/go.mod h1:RqWSrTOtEASCrGOEffq0n8pSg2KOgKYiWqFWczRSics=
github.com/notaryproject/tspclient-go v0.1.1-0.20240715235637-df25ef8d2172 h1:Q8UsmeFMzyFuMMq4dlbIRJUi7khEKXKUe2H2Hm3W92Y=
github.com/notaryproject/tspclient-go v0.1.1-0.20240715235637-df25ef8d2172/go.mod h1:LGyA/6Kwd2FlM0uk8Vc5il3j0CddbWSHBj/4kxQDbjs=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
Expand Down Expand Up @@ -73,12 +75,12 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
Expand Down
42 changes: 39 additions & 3 deletions internal/httputil/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,50 @@ import (
"oras.land/oras-go/v2/registry/remote/auth"
)

// NewAuthClient returns an *auth.Client
var userAgent = "notation/" + version.GetVersion()

// NewAuthClient returns an *auth.Client with debug log and user agent set
func NewAuthClient(ctx context.Context, httpClient *http.Client) *auth.Client {
httpClient = trace.SetHTTPDebugLog(ctx, httpClient)
client := &auth.Client{
Client: httpClient,
Cache: auth.NewCache(),
ClientID: "notation",
}
client.SetUserAgent("notation/" + version.GetVersion())
trace.SetHTTPDebugLog(ctx, client)
client.SetUserAgent(userAgent)
return client
}

// NewClient returns an *http.Client with debug log and user agent set
func NewClient(ctx context.Context, client *http.Client) *http.Client {
client = trace.SetHTTPDebugLog(ctx, client)
return SetUserAgent(client)
}

type userAgentTransport struct {
base http.RoundTripper
}

// RoundTrip returns t.Base.RoundTrip with user agent set in the request Header
func (t *userAgentTransport) RoundTrip(req *http.Request) (*http.Response, error) {
Comment thread
This conversation was marked as resolved.
r := req.Clone(req.Context())
if r.Header == nil {
r.Header = http.Header{}
}
r.Header.Set("User-Agent", userAgent)
return t.base.RoundTrip(r)
}

// SetUserAgent sets the user agent for all out-going requests.
func SetUserAgent(client *http.Client) *http.Client {
if client == nil {
client = &http.Client{}
}
if client.Transport == nil {
client.Transport = http.DefaultTransport
}
client.Transport = &userAgentTransport{
base: client.Transport,
}
return client
}
16 changes: 8 additions & 8 deletions internal/trace/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (

"github.com/notaryproject/notation-go/log"
"github.com/sirupsen/logrus"
"oras.land/oras-go/v2/registry/remote/auth"
)

// Transport is an http.RoundTripper that keeps track of the in-flight
Expand Down Expand Up @@ -87,15 +86,16 @@ func logHeader(header http.Header, e log.Logger) {
}

// SetHTTPDebugLog sets up http debug log with logrus.Logger
func SetHTTPDebugLog(ctx context.Context, authClient *auth.Client) {
func SetHTTPDebugLog(ctx context.Context, client *http.Client) *http.Client {
if logrusLog, ok := log.GetLogger(ctx).(*logrus.Logger); !ok || logrusLog.Level != logrus.DebugLevel {
return
return client
}
if authClient.Client == nil {
authClient.Client = &http.Client{}
if client == nil {
client = &http.Client{}
}
if authClient.Client.Transport == nil {
authClient.Client.Transport = http.DefaultTransport
if client.Transport == nil {
client.Transport = http.DefaultTransport
}
authClient.Client.Transport = NewTransport(authClient.Client.Transport)
client.Transport = NewTransport(client.Transport)
return client
}
Loading