-
Notifications
You must be signed in to change notification settings - Fork 537
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Buildkite provider Inspired by https://github.com/buildkite/agent/blob/main/clicommand/oidc_request_token.go, this should replicate the logic used when calling 'buildkite-agent oidc request-token' Fixes #2778 Signed-off-by: Hayden Blauzvern <hblauzvern@google.com> * Support customizing log level based on env var Signed-off-by: Hayden Blauzvern <hblauzvern@google.com> --------- Signed-off-by: Hayden Blauzvern <hblauzvern@google.com>
- Loading branch information
1 parent
b54f060
commit 6ca4ffd
Showing
6 changed files
with
130 additions
and
8 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,71 @@ | ||
// Copyright 2023 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 buildkite | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/buildkite/agent/v3/api" | ||
"github.com/buildkite/agent/v3/logger" | ||
"github.com/sigstore/cosign/v2/pkg/cosign/env" | ||
"github.com/sigstore/cosign/v2/pkg/providers" | ||
) | ||
|
||
func init() { | ||
providers.Register("buildkite-agent", &buildkiteAgent{}) | ||
} | ||
|
||
type buildkiteAgent struct{} | ||
|
||
var _ providers.Interface = (*buildkiteAgent)(nil) | ||
|
||
// Enabled implements providers.Interface | ||
func (ba *buildkiteAgent) Enabled(ctx context.Context) bool { | ||
return env.Getenv(env.VariableBuildkiteAgentAccessToken) != "" | ||
} | ||
|
||
// Provide implements providers.Interface | ||
func (ba *buildkiteAgent) Provide(ctx context.Context, audience string) (string, error) { | ||
agentToken := env.Getenv(env.VariableBuildkiteAgentAccessToken) | ||
endpoint := env.Getenv(env.VariableBuildkiteAgentEndpoint) | ||
if endpoint == "" { | ||
endpoint = "https://agent.buildkite.com/v3" | ||
} | ||
jobID := env.Getenv(env.VariableBuildkiteJobID) | ||
logLevel := env.Getenv(env.VariableBuildkiteAgentLogLevel) | ||
if logLevel == "" { | ||
logLevel = "notice" | ||
} | ||
|
||
l := logger.NewConsoleLogger(logger.NewTextPrinter(os.Stderr), os.Exit) | ||
level, err := logger.LevelFromString(logLevel) | ||
if err != nil { | ||
return "", err | ||
} | ||
l.SetLevel(level) | ||
|
||
client := api.NewClient(l, api.Config{Token: agentToken, Endpoint: endpoint}) | ||
token, response, err := client.OIDCToken(ctx, &api.OIDCTokenRequest{Audience: audience, Job: jobID}) | ||
if err != nil { | ||
return "", err | ||
} | ||
if response != nil && response.StatusCode != http.StatusOK { | ||
return "", fmt.Errorf("buildkite agent request failed with status: %s", response.Status) | ||
} | ||
return token.Token, 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,15 @@ | ||
// Copyright 2023 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 buildkite |