...

Source file src/cloud.google.com/go/auth/credentials/internal/externalaccount/url_provider.go

Documentation: cloud.google.com/go/auth/credentials/internal/externalaccount

     1  // Copyright 2023 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package externalaccount
    16  
    17  import (
    18  	"context"
    19  	"encoding/json"
    20  	"errors"
    21  	"fmt"
    22  	"net/http"
    23  
    24  	"cloud.google.com/go/auth/internal"
    25  	"cloud.google.com/go/auth/internal/credsfile"
    26  )
    27  
    28  const (
    29  	fileTypeText             = "text"
    30  	fileTypeJSON             = "json"
    31  	urlProviderType          = "url"
    32  	programmaticProviderType = "programmatic"
    33  )
    34  
    35  type urlSubjectProvider struct {
    36  	URL     string
    37  	Headers map[string]string
    38  	Format  *credsfile.Format
    39  	Client  *http.Client
    40  }
    41  
    42  func (sp *urlSubjectProvider) subjectToken(ctx context.Context) (string, error) {
    43  	req, err := http.NewRequestWithContext(ctx, "GET", sp.URL, nil)
    44  	if err != nil {
    45  		return "", fmt.Errorf("credentials: HTTP request for URL-sourced credential failed: %w", err)
    46  	}
    47  
    48  	for key, val := range sp.Headers {
    49  		req.Header.Add(key, val)
    50  	}
    51  	resp, err := sp.Client.Do(req)
    52  	if err != nil {
    53  		return "", fmt.Errorf("credentials: invalid response when retrieving subject token: %w", err)
    54  	}
    55  	defer resp.Body.Close()
    56  
    57  	respBody, err := internal.ReadAll(resp.Body)
    58  	if err != nil {
    59  		return "", fmt.Errorf("credentials: invalid body in subject token URL query: %w", err)
    60  	}
    61  	if c := resp.StatusCode; c < http.StatusOK || c >= http.StatusMultipleChoices {
    62  		return "", fmt.Errorf("credentials: status code %d: %s", c, respBody)
    63  	}
    64  
    65  	if sp.Format == nil {
    66  		return string(respBody), nil
    67  	}
    68  	switch sp.Format.Type {
    69  	case "json":
    70  		jsonData := make(map[string]interface{})
    71  		err = json.Unmarshal(respBody, &jsonData)
    72  		if err != nil {
    73  			return "", fmt.Errorf("credentials: failed to unmarshal subject token file: %w", err)
    74  		}
    75  		val, ok := jsonData[sp.Format.SubjectTokenFieldName]
    76  		if !ok {
    77  			return "", errors.New("credentials: provided subject_token_field_name not found in credentials")
    78  		}
    79  		token, ok := val.(string)
    80  		if !ok {
    81  			return "", errors.New("credentials: improperly formatted subject token")
    82  		}
    83  		return token, nil
    84  	case fileTypeText:
    85  		return string(respBody), nil
    86  	default:
    87  		return "", errors.New("credentials: invalid credential_source file format type: " + sp.Format.Type)
    88  	}
    89  }
    90  
    91  func (sp *urlSubjectProvider) providerType() string {
    92  	return urlProviderType
    93  }
    94  

View as plain text