Skip to content

Commit

Permalink
adding initial draft gitpod provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ChevronTango committed May 19, 2023
1 parent da604ca commit 6fb1e0c
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/providers/gitpod/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// 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 gitpod defines an implementation of the providers.Interface
// that reads identity tokens from the gitpod API within a workspace.
package gitpod
81 changes: 81 additions & 0 deletions pkg/providers/gitpod/gitpod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// 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 gitpod

import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"time"

"github.com/sigstore/cosign/v2/pkg/cosign/env"
"github.com/sigstore/cosign/v2/pkg/providers"
)

func init() {
providers.Register("filesystem", &gitpod{})
}

type gitpod struct{}

var _ providers.Interface = (*gitpod)(nil)

// Enabled implements providers.Interface
func (ga *gitpod) Enabled(_ context.Context) bool {
if env.Getenv(env.VariableGitHubRequestToken) == "" {
return false
}
if env.Getenv(env.VariableGitHubRequestURL) == "" {
return false
}
return true
}

// Provide implements providers.Interface
func (ga *gitpod) Provide(ctx context.Context, audience string) (string, error) {
url := env.Getenv(env.VariableGitHubRequestURL) + "&audience=" + audience

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}

// Retry up to 3 times.
for i := 0; ; i++ {
req.Header.Add("Authorization", "bearer "+env.Getenv(env.VariableGitHubRequestToken))
resp, err := http.DefaultClient.Do(req)
if err != nil {
if i == 2 {
return "", err
}
fmt.Fprintf(os.Stderr, "error fetching GitHub OIDC token (will retry): %v\n", err)
time.Sleep(time.Second)
continue
}
defer resp.Body.Close()

var payload struct {
Value string `json:"value"`
}
decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(&payload); err != nil {
return "", err
}
return payload.Value, nil
}
}

0 comments on commit 6fb1e0c

Please sign in to comment.