|
| 1 | +// |
| 2 | +// Copyright 2025 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package oidc |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + |
| 23 | + "github.com/coreos/go-oidc/v3/oidc" |
| 24 | + "github.com/rs/zerolog" |
| 25 | +) |
| 26 | + |
| 27 | +// GitlabTokenEnv is the environment variable name for Gitlab OIDC token. |
| 28 | +// #nosec G101 - This is just the name of an environment variable, not a credential |
| 29 | +const GitlabTokenEnv = "GITLAB_OIDC" |
| 30 | + |
| 31 | +// CIServerURLEnv is the environment variable name for Gitlab CI server URL. |
| 32 | +const CIServerURLEnv = "CI_SERVER_URL" |
| 33 | + |
| 34 | +type GitlabToken struct { |
| 35 | + oidc.IDToken |
| 36 | + |
| 37 | + // ConfigRefURI is a reference to the current job workflow. |
| 38 | + ConfigRefURI string `json:"ci_config_ref_uri"` |
| 39 | + |
| 40 | + // RunnerEnvironment is the environment the runner is running in. |
| 41 | + RunnerEnvironment string `json:"runner_environment"` |
| 42 | +} |
| 43 | + |
| 44 | +type GitlabOIDCClient struct { |
| 45 | + Token *GitlabToken |
| 46 | +} |
| 47 | + |
| 48 | +func NewGitlabClient(ctx context.Context, logger *zerolog.Logger) (*GitlabOIDCClient, error) { |
| 49 | + var c GitlabOIDCClient |
| 50 | + |
| 51 | + // retrieve the Gitlab server on which the pipeline is running, which is the provider URL |
| 52 | + providerURL := os.Getenv(CIServerURLEnv) |
| 53 | + logger.Debug().Str("providerURL", providerURL).Msg("retrieved provider URL") |
| 54 | + if providerURL == "" { |
| 55 | + return nil, fmt.Errorf("%s environment variable not set", CIServerURLEnv) |
| 56 | + } |
| 57 | + |
| 58 | + tokenContent := os.Getenv(GitlabTokenEnv) |
| 59 | + logger.Debug().Msg("retrieved token content") |
| 60 | + if tokenContent == "" { |
| 61 | + return nil, fmt.Errorf("%s environment variable not set", GitlabTokenEnv) |
| 62 | + } |
| 63 | + |
| 64 | + token, err := parseToken(ctx, providerURL, tokenContent) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("failed to parse token: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + c.Token = token |
| 70 | + return &c, nil |
| 71 | +} |
| 72 | + |
| 73 | +func parseToken(ctx context.Context, providerURL string, tokenString string) (*GitlabToken, error) { |
| 74 | + provider, err := oidc.NewProvider(ctx, providerURL) |
| 75 | + if err != nil { |
| 76 | + return nil, fmt.Errorf("failed to connect to OIDC provider: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + verifier := provider.Verifier(&oidc.Config{ |
| 80 | + SkipClientIDCheck: true, // Skip client ID check since we're just parsing |
| 81 | + }) |
| 82 | + |
| 83 | + idToken, err := verifier.Verify(ctx, tokenString) |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("token verification failed: %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + token := &GitlabToken{ |
| 89 | + IDToken: *idToken, |
| 90 | + } |
| 91 | + |
| 92 | + // Extract claims to populate our custom fields |
| 93 | + var claims map[string]interface{} |
| 94 | + if err := idToken.Claims(&claims); err != nil { |
| 95 | + return nil, fmt.Errorf("failed to extract claims: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + if configRefURI, ok := claims["ci_config_ref_uri"].(string); ok { |
| 99 | + token.ConfigRefURI = configRefURI |
| 100 | + } |
| 101 | + |
| 102 | + if runnerEnv, ok := claims["runner_environment"].(string); ok { |
| 103 | + token.RunnerEnvironment = runnerEnv |
| 104 | + } |
| 105 | + |
| 106 | + return token, nil |
| 107 | +} |
0 commit comments