-
Notifications
You must be signed in to change notification settings - Fork 596
add github&gitlab reference support to generate-key-pair #848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4f47f51
feat: add github support to generate-key-pair command
erkanzileli 3434196
feat: gitlab integration
developer-guy 6d63e6b
feat: add github/gitlab to generate-key-pair command's description
developer-guy b4e834c
feat: fix docgen
developer-guy ea0d5ea
feat: enhance messages,gitlab work with project id
developer-guy 6a635e2
feat: generate doc
developer-guy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,37 @@ | ||
// | ||
// 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 git | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sigstore/cosign/pkg/cosign" | ||
"github.com/sigstore/cosign/pkg/cosign/git/github" | ||
"github.com/sigstore/cosign/pkg/cosign/git/gitlab" | ||
) | ||
|
||
var providerMap = map[string]Git{ | ||
github.ReferenceScheme: github.New(), | ||
gitlab.ReferenceScheme: gitlab.New(), | ||
} | ||
|
||
type Git interface { | ||
PutSecret(ctx context.Context, ref string, pf cosign.PassFunc) error | ||
} | ||
|
||
func GetProvider(provider string) Git { | ||
return providerMap[provider] | ||
} |
This file contains hidden or 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,119 @@ | ||
// | ||
// 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 github | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/google/go-github/v39/github" | ||
"github.com/pkg/errors" | ||
"github.com/sigstore/cosign/pkg/cosign" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
const ( | ||
ReferenceScheme = "github" | ||
) | ||
|
||
type Gh struct{} | ||
|
||
func New() *Gh { | ||
return &Gh{} | ||
} | ||
|
||
func (g *Gh) PutSecret(ctx context.Context, ref string, pf cosign.PassFunc) error { | ||
keys, err := cosign.GenerateKeyPair(pf) | ||
if err != nil { | ||
return errors.Wrap(err, "generating key pair") | ||
} | ||
|
||
var httpClient *http.Client | ||
if token, ok := os.LookupEnv("GITHUB_TOKEN"); ok { | ||
ts := oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: token}, | ||
) | ||
httpClient = oauth2.NewClient(ctx, ts) | ||
} else { | ||
return errors.New("could not find \"GITHUB_TOKEN\" env variable") | ||
} | ||
client := github.NewClient(httpClient) | ||
|
||
split := strings.Split(ref, "/") | ||
if len(split) < 2 { | ||
return errors.New("could not parse scheme, use github://<owner>/<repo> format") | ||
} | ||
owner, repo := split[0], split[1] | ||
|
||
key, getRepoPubKeyResp, err := client.Actions.GetRepoPublicKey(ctx, owner, repo) | ||
if err != nil { | ||
return errors.Wrap(err, "could not get repository public key") | ||
} | ||
|
||
if getRepoPubKeyResp.StatusCode < 200 && getRepoPubKeyResp.StatusCode >= 300 { | ||
bodyBytes, _ := io.ReadAll(getRepoPubKeyResp.Body) | ||
return fmt.Errorf("%s", bodyBytes) | ||
} | ||
|
||
passwordSecretEnv := &github.EncryptedSecret{ | ||
Name: "COSIGN_PASSWORD", | ||
KeyID: key.GetKeyID(), | ||
EncryptedValue: base64.StdEncoding.EncodeToString(keys.Password()), | ||
} | ||
|
||
passwordSecretEnvResp, err := client.Actions.CreateOrUpdateRepoSecret(ctx, owner, repo, passwordSecretEnv) | ||
if err != nil { | ||
return errors.Wrap(err, "could not create \"COSIGN_PASSWORD\" environment variable") | ||
} | ||
|
||
if passwordSecretEnvResp.StatusCode < 200 && passwordSecretEnvResp.StatusCode >= 300 { | ||
bodyBytes, _ := io.ReadAll(passwordSecretEnvResp.Body) | ||
return fmt.Errorf("%s", bodyBytes) | ||
} | ||
|
||
fmt.Fprintln(os.Stderr, "Private key written to COSIGN_PASSWORD environment variable") | ||
|
||
privateKeySecretEnv := &github.EncryptedSecret{ | ||
Name: "COSIGN_PRIVATE_KEY", | ||
KeyID: key.GetKeyID(), | ||
EncryptedValue: base64.StdEncoding.EncodeToString(keys.PrivateBytes), | ||
} | ||
|
||
repoSecretEnvResp, err := client.Actions.CreateOrUpdateRepoSecret(ctx, owner, repo, privateKeySecretEnv) | ||
if err != nil { | ||
return errors.Wrap(err, "could not create \"COSIGN_PRIVATE_KEY\" environment variable") | ||
} | ||
|
||
if repoSecretEnvResp.StatusCode < 200 && repoSecretEnvResp.StatusCode >= 300 { | ||
bodyBytes, _ := io.ReadAll(repoSecretEnvResp.Body) | ||
return fmt.Errorf("%s", bodyBytes) | ||
} | ||
|
||
fmt.Fprintln(os.Stderr, "Private key written to COSIGN_PRIVATE_KEY environment variable") | ||
|
||
if err := ioutil.WriteFile("cosign.pub", keys.PublicBytes, 0o600); err != nil { | ||
return err | ||
} | ||
fmt.Fprintln(os.Stderr, "Public key written to cosign.pub") | ||
|
||
return nil | ||
} |
This file contains hidden or 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,108 @@ | ||
// | ||
// 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 gitlab | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sigstore/cosign/pkg/cosign" | ||
"github.com/xanzy/go-gitlab" | ||
) | ||
|
||
const ( | ||
ReferenceScheme = "gitlab" | ||
) | ||
|
||
type Gl struct{} | ||
|
||
func New() *Gl { | ||
return &Gl{} | ||
} | ||
|
||
func (g *Gl) PutSecret(ctx context.Context, ref string, pf cosign.PassFunc) error { | ||
keys, err := cosign.GenerateKeyPair(pf) | ||
if err != nil { | ||
return errors.Wrap(err, "generating key pair") | ||
} | ||
|
||
token, tokenExists := os.LookupEnv("GITLAB_TOKEN") | ||
|
||
if !tokenExists { | ||
return errors.New("could not find \"GITLAB_TOKEN\"") | ||
} | ||
|
||
var client *gitlab.Client | ||
if url, baseURLExists := os.LookupEnv("GITLAB_BASE_URL"); baseURLExists { | ||
client, err = gitlab.NewClient(token, gitlab.WithBaseURL(url)) | ||
if err != nil { | ||
return errors.Wrap(err, "could not create GitLab client") | ||
} | ||
} else { | ||
client, err = gitlab.NewClient(token) | ||
if err != nil { | ||
return errors.Wrap(err, "could not create GitLab client") | ||
} | ||
} | ||
|
||
_, passwordResp, err := client.ProjectVariables.CreateVariable(ref, &gitlab.CreateProjectVariableOptions{ | ||
Key: gitlab.String("COSIGN_PASSWORD"), | ||
Value: gitlab.String(string(keys.Password())), | ||
VariableType: gitlab.VariableType(gitlab.EnvVariableType), | ||
Protected: gitlab.Bool(false), | ||
Masked: gitlab.Bool(false), | ||
EnvironmentScope: gitlab.String("*"), | ||
}) | ||
if err != nil { | ||
return errors.Wrap(err, "could not create \"COSIGN_PASSWORD\" variable") | ||
} | ||
|
||
if passwordResp.StatusCode < 200 && passwordResp.StatusCode >= 300 { | ||
bodyBytes, _ := io.ReadAll(passwordResp.Body) | ||
return errors.Errorf("%s", bodyBytes) | ||
} | ||
|
||
fmt.Fprintln(os.Stderr, "Password written to \"COSIGN_PASSWORD\" variable") | ||
|
||
_, privateKeyResp, err := client.ProjectVariables.CreateVariable(ref, &gitlab.CreateProjectVariableOptions{ | ||
Key: gitlab.String("COSIGN_PRIVATE_KEY"), | ||
Value: gitlab.String(string(keys.PrivateBytes)), | ||
VariableType: gitlab.VariableType(gitlab.EnvVariableType), | ||
Protected: gitlab.Bool(false), | ||
Masked: gitlab.Bool(false), | ||
}) | ||
if err != nil { | ||
return errors.Wrap(err, "could not create \"COSIGN_PRIVATE_KEY\" variable") | ||
} | ||
|
||
if privateKeyResp.StatusCode < 200 && privateKeyResp.StatusCode >= 300 { | ||
bodyBytes, _ := io.ReadAll(privateKeyResp.Body) | ||
return errors.Errorf("%s", bodyBytes) | ||
} | ||
|
||
fmt.Fprintln(os.Stderr, "Private key written to \"COSIGN_PRIVATE_KEY\" variable") | ||
|
||
if err := ioutil.WriteFile("cosign.pub", keys.PublicBytes, 0o600); err != nil { | ||
return err | ||
} | ||
fmt.Fprintln(os.Stderr, "Public key written to cosign.pub") | ||
|
||
return nil | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.