go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/config/helpers.go (about)

     1  // Copyright 2023 The LUCI Authors.
     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 config
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"io"
    21  	"net/http"
    22  
    23  	"github.com/klauspost/compress/gzip"
    24  )
    25  
    26  // DownloadConfigFromSignedURL downloads the config file content from the
    27  // provided GCS signed url. The signed url is generated by Config Service V2.
    28  func DownloadConfigFromSignedURL(ctx context.Context, client *http.Client, signedURL string) ([]byte, error) {
    29  	if client == nil {
    30  		return nil, fmt.Errorf("http client is nil")
    31  	}
    32  	if signedURL == "" {
    33  		return nil, fmt.Errorf("empty signedURL")
    34  	}
    35  
    36  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, signedURL, http.NoBody)
    37  	if err != nil {
    38  		return nil, fmt.Errorf("failed to create http request to download file: %w", err)
    39  	}
    40  	req.Header.Add("Accept-Encoding", "gzip")
    41  	res, err := client.Do(req)
    42  	if err != nil {
    43  		return nil, fmt.Errorf("failed to execute http request to download file: %w", err)
    44  	}
    45  
    46  	defer func() { _ = res.Body.Close() }()
    47  
    48  	switch {
    49  	case res.StatusCode != http.StatusOK:
    50  		body, err := io.ReadAll(res.Body)
    51  		if err != nil {
    52  			return nil, fmt.Errorf("failed to read response body: %w", err)
    53  		}
    54  		return nil, fmt.Errorf("failed to download file, got http response code: %d, body: %q", res.StatusCode, string(body))
    55  
    56  	case res.Header.Get("Content-Encoding") == "gzip":
    57  		reader, err := gzip.NewReader(res.Body)
    58  		if err != nil {
    59  			return nil, fmt.Errorf("failed to create gzip reader: %w", err)
    60  		}
    61  		ret, err := io.ReadAll(reader)
    62  		if err != nil {
    63  			_ = reader.Close()
    64  			return nil, fmt.Errorf("failed to read or decompress response body: %w", err)
    65  		}
    66  		if err := reader.Close(); err != nil {
    67  			return nil, fmt.Errorf("failed to close gzip reader: %w", err)
    68  		}
    69  		return ret, nil
    70  
    71  	default:
    72  		ret, err := io.ReadAll(res.Body)
    73  		if err != nil {
    74  			return nil, fmt.Errorf("failed to read response body: %w", err)
    75  		}
    76  		return ret, nil
    77  	}
    78  }