|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | +) |
| 9 | + |
| 10 | +var ( |
| 11 | + DEEPSOURCE_AUDIENCE = "DeepSource" |
| 12 | + ALLOWED_PROVIDERS = map[string]bool{ |
| 13 | + "github-actions": true, |
| 14 | + } |
| 15 | +) |
| 16 | + |
| 17 | +// FetchOIDCTokenFromProvider fetches the OIDC token from the OIDC token provider. |
| 18 | +// It takes the request ID and the request URL as input and returns the OIDC token as a string. |
| 19 | +func FetchOIDCTokenFromProvider(requestId, requestUrl string) (string, error) { |
| 20 | + // requestid is the bearer token that needs to be sent to the request url |
| 21 | + req, err := http.NewRequest("GET", requestUrl, nil) |
| 22 | + if err != nil { |
| 23 | + return "", err |
| 24 | + } |
| 25 | + req.Header.Set("Authorization", "Bearer "+requestId) |
| 26 | + // set the expected audiences as the audience parameter |
| 27 | + q := req.URL.Query() |
| 28 | + q.Set("audience", DEEPSOURCE_AUDIENCE) |
| 29 | + req.URL.RawQuery = q.Encode() |
| 30 | + |
| 31 | + // send the request |
| 32 | + client := &http.Client{} |
| 33 | + resp, err := client.Do(req) |
| 34 | + if err != nil { |
| 35 | + return "", err |
| 36 | + } |
| 37 | + defer resp.Body.Close() |
| 38 | + |
| 39 | + // check if the response is 200 |
| 40 | + if resp.StatusCode != http.StatusOK { |
| 41 | + return "", fmt.Errorf("failed to fetch OIDC token: %s", resp.Status) |
| 42 | + } |
| 43 | + |
| 44 | + // extract the token from the json response. The token is sent under the key `value` |
| 45 | + // and the response is a json object |
| 46 | + var tokenResponse struct { |
| 47 | + Value string `json:"value"` |
| 48 | + } |
| 49 | + if err := json.NewDecoder(resp.Body).Decode(&tokenResponse); err != nil { |
| 50 | + return "", err |
| 51 | + } |
| 52 | + // check if the token is empty |
| 53 | + if tokenResponse.Value == "" { |
| 54 | + return "", fmt.Errorf("failed to fetch OIDC token: empty token") |
| 55 | + } |
| 56 | + // return the token |
| 57 | + return tokenResponse.Value, nil |
| 58 | +} |
| 59 | + |
| 60 | +// ExchangeOIDCTokenForTempDSN exchanges the OIDC token for a temporary DSN. |
| 61 | +// It sends the OIDC token to the respective DeepSource API endpoint and returns the temp DSN as string. |
| 62 | +func ExchangeOIDCTokenForTempDSN(oidcToken, dsEndpoint, provider string) (string, error) { |
| 63 | + apiEndpoint := fmt.Sprintf("%s/services/oidc/%s/", dsEndpoint, provider) |
| 64 | + req, err := http.NewRequest("POST", apiEndpoint, nil) |
| 65 | + if err != nil { |
| 66 | + return "", err |
| 67 | + } |
| 68 | + req.Header.Set("Authorization", "Bearer "+oidcToken) |
| 69 | + |
| 70 | + type ExchangeResponse struct { |
| 71 | + DSN string `json:"access_token"` |
| 72 | + } |
| 73 | + resp, err := http.DefaultClient.Do(req) |
| 74 | + if err != nil { |
| 75 | + return "", err |
| 76 | + } |
| 77 | + defer resp.Body.Close() |
| 78 | + if resp.StatusCode != http.StatusOK { |
| 79 | + return "", fmt.Errorf("failed to exchange OIDC token for DSN: %s", resp.Status) |
| 80 | + } |
| 81 | + var exchangeResponse ExchangeResponse |
| 82 | + if err := json.NewDecoder(resp.Body).Decode(&exchangeResponse); err != nil { |
| 83 | + return "", err |
| 84 | + } |
| 85 | + // check if the token is empty |
| 86 | + if exchangeResponse.DSN == "" { |
| 87 | + return "", fmt.Errorf("failed to exchange OIDC token for DSN: empty token") |
| 88 | + } |
| 89 | + // return the token |
| 90 | + return exchangeResponse.DSN, nil |
| 91 | +} |
| 92 | + |
| 93 | +func GetDSNFromOIDC(requestId, requestUrl, dsEndpoint, provider string) (string, error) { |
| 94 | + // infer provider from environment variables. |
| 95 | + // Github actions sets the GITHUB_ACTIONS environment variable to true by default. |
| 96 | + if os.Getenv("GITHUB_ACTIONS") == "true" { |
| 97 | + provider = "github-actions" |
| 98 | + } |
| 99 | + |
| 100 | + if dsEndpoint == "" { |
| 101 | + return "", fmt.Errorf("--deepsource-host-endpoint can not be empty") |
| 102 | + } |
| 103 | + |
| 104 | + if provider == "" { |
| 105 | + return "", fmt.Errorf("--oidc-provider can not be empty") |
| 106 | + } |
| 107 | + |
| 108 | + isSupported := ALLOWED_PROVIDERS[provider] |
| 109 | + if !isSupported { |
| 110 | + return "", fmt.Errorf("provider %s is not supported for OIDC Token exchange (Supported Providers: %v)", provider, ALLOWED_PROVIDERS) |
| 111 | + } |
| 112 | + if requestId == "" || requestUrl == "" { |
| 113 | + var foundIDToken, foundRequestURL bool |
| 114 | + // try to fetch the token from the environment variables. |
| 115 | + // skipcq: CRT-A0014 |
| 116 | + switch provider { |
| 117 | + case "github-actions": |
| 118 | + requestId, foundIDToken = os.LookupEnv("ACTIONS_ID_TOKEN_REQUEST_TOKEN") |
| 119 | + requestUrl, foundRequestURL = os.LookupEnv("ACTIONS_ID_TOKEN_REQUEST_URL") |
| 120 | + if !(foundIDToken && foundRequestURL) { |
| 121 | + errMsg := `failed to fetch "ACTIONS_ID_TOKEN_REQUEST_TOKEN" and "ACTIONS_ID_TOKEN_REQUEST_URL" from environment variables. Please make sure you are running this in a GitHub Actions environment with the required permissions. Or, use '--oidc-request-token' and '--oidc-request-url' flags to pass the token and request URL` |
| 122 | + return "", fmt.Errorf("%s", errMsg) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + oidcToken, err := FetchOIDCTokenFromProvider(requestId, requestUrl) |
| 127 | + if err != nil { |
| 128 | + return "", err |
| 129 | + } |
| 130 | + tempDSN, err := ExchangeOIDCTokenForTempDSN(oidcToken, dsEndpoint, provider) |
| 131 | + if err != nil { |
| 132 | + return "", err |
| 133 | + } |
| 134 | + return tempDSN, nil |
| 135 | +} |
0 commit comments