| 1 |
// Copyright 2024 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 cert |
| 16 |
|
| 17 |
import ( |
| 18 |
"crypto/tls" |
| 19 |
"encoding/json" |
| 20 |
"errors" |
| 21 |
"io" |
| 22 |
"os" |
| 23 |
|
| 24 |
"github.com/googleapis/enterprise-certificate-proxy/client/util" |
| 25 |
) |
| 26 |
|
| 27 |
type certConfigs struct { |
| 28 |
Workload *workloadSource `json:"workload"` |
| 29 |
} |
| 30 |
|
| 31 |
type workloadSource struct { |
| 32 |
CertPath string `json:"cert_path"` |
| 33 |
KeyPath string `json:"key_path"` |
| 34 |
} |
| 35 |
|
| 36 |
type certificateConfig struct { |
| 37 |
CertConfigs certConfigs `json:"cert_configs"` |
| 38 |
} |
| 39 |
|
| 40 |
// getconfigFilePath determines the path to the certificate configuration file. |
| 41 |
// It first checks for the presence of an environment variable that specifies |
| 42 |
// the file path. If the environment variable is not set, it falls back to |
| 43 |
// a default configuration file path. |
| 44 |
func getconfigFilePath() string { |
| 45 |
envFilePath := util.GetConfigFilePathFromEnv() |
| 46 |
if envFilePath != "" { |
| 47 |
return envFilePath |
| 48 |
} |
| 49 |
return util.GetDefaultConfigFilePath() |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
// GetCertificatePath retrieves the certificate file path from the provided |
| 54 |
// configuration file. If the configFilePath is empty, it attempts to load |
| 55 |
// the configuration from a well-known gcloud location. |
| 56 |
// This function is exposed to allow other packages, such as the |
| 57 |
// externalaccount package, to retrieve the certificate path without needing |
| 58 |
// to load the entire certificate configuration. |
| 59 |
func GetCertificatePath(configFilePath string) (string, error) { |
| 60 |
if configFilePath == "" { |
| 61 |
configFilePath = getconfigFilePath() |
| 62 |
} |
| 63 |
certFile, _, err := getCertAndKeyFiles(configFilePath) |
| 64 |
if err != nil { |
| 65 |
return "", err |
| 66 |
} |
| 67 |
return certFile, nil |
| 68 |
} |
| 69 |
|
| 70 |
// NewWorkloadX509CertProvider creates a certificate source |
| 71 |
// that reads a certificate and private key file from the local file system. |
| 72 |
// This is intended to be used for workload identity federation. |
| 73 |
// |
| 74 |
// The configFilePath points to a config file containing relevant parameters |
| 75 |
// such as the certificate and key file paths. |
| 76 |
// If configFilePath is empty, the client will attempt to load the config from |
| 77 |
// a well-known gcloud location. |
| 78 |
func NewWorkloadX509CertProvider(configFilePath string) (Provider, error) { |
| 79 |
if configFilePath == "" { |
| 80 |
configFilePath = getconfigFilePath() |
| 81 |
} |
| 82 |
certFile, keyFile, err := getCertAndKeyFiles(configFilePath) |
| 83 |
if err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
|
| 87 |
source := &workloadSource{ |
| 88 |
CertPath: certFile, |
| 89 |
KeyPath: keyFile, |
| 90 |
} |
| 91 |
return source.getClientCertificate, nil |
| 92 |
} |
| 93 |
|
| 94 |
// getClientCertificate attempts to load the certificate and key from the files specified in the |
| 95 |
// certificate config. |
| 96 |
func (s *workloadSource) getClientCertificate(info *tls.CertificateRequestInfo) (*tls.Certificate, error) { |
| 97 |
cert, err := tls.LoadX509KeyPair(s.CertPath, s.KeyPath) |
| 98 |
if err != nil { |
| 99 |
return nil, err |
| 100 |
} |
| 101 |
return &cert, nil |
| 102 |
} |
| 103 |
|
| 104 |
// getCertAndKeyFiles attempts to read the provided config file and return the certificate and private |
| 105 |
// key file paths. |
| 106 |
func getCertAndKeyFiles(configFilePath string) (string, string, error) { |
| 107 |
jsonFile, err := os.Open(configFilePath) |
| 108 |
if err != nil { |
| 109 |
return "", "", errSourceUnavailable |
| 110 |
} |
| 111 |
|
| 112 |
byteValue, err := io.ReadAll(jsonFile) |
| 113 |
if err != nil { |
| 114 |
return "", "", err |
| 115 |
} |
| 116 |
|
| 117 |
var config certificateConfig |
| 118 |
if err := json.Unmarshal(byteValue, &config); err != nil { |
| 119 |
return "", "", err |
| 120 |
} |
| 121 |
|
| 122 |
if config.CertConfigs.Workload == nil { |
| 123 |
return "", "", errSourceUnavailable |
| 124 |
} |
| 125 |
|
| 126 |
certFile := config.CertConfigs.Workload.CertPath |
| 127 |
keyFile := config.CertConfigs.Workload.KeyPath |
| 128 |
|
| 129 |
if certFile == "" { |
| 130 |
return "", "", errors.New("certificate configuration is missing the certificate file location") |
| 131 |
} |
| 132 |
|
| 133 |
if keyFile == "" { |
| 134 |
return "", "", errors.New("certificate configuration is missing the key file location") |
| 135 |
} |
| 136 |
|
| 137 |
return certFile, keyFile, nil |
| 138 |
} |