-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
send User-Agent string w/ rekor, fulcio, and ggcr HTTP requests (#1131)
* send User-Agent string w/ rekor, fulcio, and ggcr HTTP requests Signed-off-by: Jake Sanders <[email protected]> * centralize rekor client construction Signed-off-by: Jake Sanders <[email protected]> * also migrate policy-init Signed-off-by: Jake Sanders <[email protected]> * add tests to `fulcio.NewClient` Signed-off-by: Jake Sanders <[email protected]>
- Loading branch information
Jake Sanders
committed
Dec 4, 2021
1 parent
dbb2a17
commit 7e5ff00
Showing
16 changed files
with
230 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2021 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package options | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
|
||
"github.com/sigstore/cosign/pkg/version" | ||
) | ||
|
||
var ( | ||
// uaString is meant to resemble the User-Agent sent by browsers with requests. | ||
// See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent | ||
uaString = fmt.Sprintf("cosign/%s (%s; %s)", version.GitVersion, runtime.GOOS, runtime.GOARCH) | ||
) | ||
|
||
// UserAgent returns the User-Agent string which `cosign` should send with HTTP requests.ß | ||
func UserAgent() string { | ||
return uaString | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2021 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package rekor | ||
|
||
import ( | ||
rekor "github.com/sigstore/rekor/pkg/client" | ||
"github.com/sigstore/rekor/pkg/generated/client" | ||
|
||
"github.com/sigstore/cosign/cmd/cosign/cli/options" | ||
) | ||
|
||
func NewClient(rekorURL string) (*client.Rekor, error) { | ||
rekorClient, err := rekor.GetRekorClient(rekorURL, rekor.WithUserAgent(options.UserAgent())) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return rekorClient, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2021 The Sigstore Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package rekor | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/sigstore/cosign/cmd/cosign/cli/options" | ||
) | ||
|
||
func TestNewClient(t *testing.T) { | ||
t.Parallel() | ||
expectedUserAgent := options.UserAgent() | ||
requestReceived := false | ||
testServer := httptest.NewServer(http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
requestReceived = true | ||
file := []byte{} | ||
|
||
got := r.UserAgent() | ||
if got != expectedUserAgent { | ||
t.Errorf("wanted User-Agent %q, got %q", expectedUserAgent, got) | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
_, _ = w.Write(file) | ||
})) | ||
defer testServer.Close() | ||
|
||
client, err := NewClient(testServer.URL) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
_, _ = client.Tlog.GetLogInfo(nil) | ||
|
||
if !requestReceived { | ||
t.Fatal("no requests were received") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.