Skip to content

Commit b818c7e

Browse files
committed
fix: ensuring environment variables are honoured
Signed-off-by: Dario Tranchitella <dario@tranchitella.eu>
1 parent e74345f commit b818c7e

3 files changed

Lines changed: 51 additions & 35 deletions

File tree

docs/sidecar-deployment.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ spec:
9292
- name: grpc
9393
containerPort: 50001
9494
protocol: TCP
95+
env:
96+
- name: TALOS_TOKEN
97+
valueFrom:
98+
secretKeyRef:
99+
name: ${CLUSTER_NAME}-talos-ca
100+
key: token
95101
volumeMounts:
96102
- name: talos-ca
97103
mountPath: /etc/talos-ca

docs/standalone-deployment.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ spec:
9797
containerPort: 50001
9898
protocol: TCP
9999
hostPort: 50001
100+
env:
101+
- name: TALOS_TOKEN
102+
valueFrom:
103+
secretKeyRef:
104+
name: ${CLUSTER_NAME}-talos-ca
105+
key: token
100106
volumeMounts:
101107
- name: talos-ca
102108
mountPath: /etc/talos-ca

main.go

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,47 @@ import (
2727
"github.com/clastix/talos-csr-signer/pkg/server"
2828
)
2929

30-
func main() {
31-
var port int
32-
33-
var caCertPath, caKeyPath, tlsCertPath, tlsKeyPath, token string
30+
const (
31+
cliPortName = "port"
32+
cliCACertificatePath = "ca-cert-path"
33+
cliCAPrivateKeyPath = "ca-key-path"
34+
cliTLSCertificatePath = "tls-cert-path"
35+
cliTLSPrivateKeyPath = "tls-key-path"
36+
cliTalosToken = "talos-token"
37+
)
3438

39+
func main() {
3540
rootCmd := &cobra.Command{
3641
Use: "talos-csr-signer",
3742
Short: "gRPC server for signing Talos CSR",
3843
PreRunE: func(*cobra.Command, []string) error {
3944
switch {
40-
case port <= 0:
45+
case viper.GetInt(cliPortName) <= 0:
4146
return pkgerrors.ErrMissingPort
42-
case port > 65535:
47+
case viper.GetInt(cliPortName) > 65535:
4348
return pkgerrors.ErrPortOutOfRange
44-
case token == "":
49+
case viper.GetString(cliTalosToken) == "":
4550
return pkgerrors.ErrMissingToken
46-
case caCertPath == "":
51+
case viper.GetString(cliCACertificatePath) == "":
4752
return errors.Wrap(pkgerrors.ErrMissingPath, "CA certificate path is missing")
48-
case caKeyPath == "":
53+
case viper.GetString(cliCAPrivateKeyPath) == "":
4954
return errors.Wrap(pkgerrors.ErrMissingPath, "CA private key path is missing")
50-
case tlsCertPath == "":
55+
case viper.GetString(cliTLSCertificatePath) == "":
5156
return errors.Wrap(pkgerrors.ErrMissingPath, "server certificate path is missing")
52-
case tlsKeyPath == "":
57+
case viper.GetString(cliTLSPrivateKeyPath) == "":
5358
return errors.Wrap(pkgerrors.ErrMissingPath, "server private key path is missing")
5459
}
5560

5661
return nil
5762
},
5863
RunE: func(*cobra.Command, []string) error {
5964
// Load CA certificate
60-
caCertPEM, caCertErr := os.ReadFile(caCertPath) //nolint:gosec
65+
caCertPEM, caCertErr := os.ReadFile(viper.GetString(cliCACertificatePath))
6166
if caCertErr != nil {
6267
return errors.Wrap(pkgerrors.ErrReadFile, "failed to read CA certificate: "+caCertErr.Error())
6368
}
6469
// Load CA private key
65-
caKeyPEM, caKeyErr := os.ReadFile(caKeyPath) //nolint:gosec
70+
caKeyPEM, caKeyErr := os.ReadFile(viper.GetString(cliCAPrivateKeyPath))
6671
if caKeyErr != nil {
6772
return errors.Wrap(pkgerrors.ErrReadFile, "failed to read CA private key: "+caKeyErr.Error())
6873
}
@@ -92,25 +97,24 @@ func main() {
9297
return errors.Wrap(pkgerrors.ErrParseCertificate, privateKeyErr.Error())
9398
}
9499

95-
cert, crtErr := tls.LoadX509KeyPair(tlsCertPath, tlsKeyPath)
100+
cert, crtErr := tls.LoadX509KeyPair(viper.GetString(cliTLSCertificatePath), viper.GetString(cliTLSPrivateKeyPath))
96101
if crtErr != nil {
97102
return errors.Wrap(pkgerrors.ErrLoadingCertificate, crtErr.Error())
98103
}
99-
100104
// Create TLS credentials
101105
tlsConfig := &tls.Config{ //nolint:gosec
102106
Certificates: []tls.Certificate{cert},
103107
ClientAuth: tls.NoClientCert, // Don't require client certificates
104108
}
105109
creds := credentials.NewTLS(tlsConfig)
106-
107110
// Create gRPC Server with TLS
108111
srv := &server.Server{
109112
CACert: caCertPEM,
110113
CAPrivateKey: caPrivateKey,
111-
ValidToken: token,
114+
ValidToken: viper.GetString(cliTalosToken),
112115
}
113116

117+
port := viper.GetInt(cliPortName)
114118
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
115119
if err != nil {
116120
return errors.Wrap(pkgerrors.ErrServerListen, fmt.Sprintf("%d: %s", port, err.Error()))
@@ -130,29 +134,29 @@ func main() {
130134
}
131135

132136
// Flags with their defaults
133-
rootCmd.Flags().IntVar(&port, "port", 50001, "Port to listen on")
134-
rootCmd.Flags().StringVar(&caCertPath, "ca-cert-path", "/etc/talos-ca/tls.crt", "Path to CA certificate")
135-
rootCmd.Flags().StringVar(&caKeyPath, "ca-key-path", "/etc/talos-ca/tls.key", "Path to CA private key")
136-
rootCmd.Flags().StringVar(&tlsCertPath, "tls-cert-path", "/etc/talos-server-crt/tls.crt", "Path to the Server TLS certificate")
137-
rootCmd.Flags().StringVar(&tlsKeyPath, "tls-key-path", "/etc/talos-server-crt/tls.key", "Path to Server TLS private key")
138-
rootCmd.Flags().StringVar(&token, "talos-token", "", "Talos token")
137+
rootCmd.Flags().Int(cliPortName, 50001, "Port to listen on")
138+
rootCmd.Flags().String(cliCACertificatePath, "/etc/talos-ca/tls.crt", "Path to CA certificate")
139+
rootCmd.Flags().String(cliCAPrivateKeyPath, "/etc/talos-ca/tls.key", "Path to CA private key")
140+
rootCmd.Flags().String(cliTLSCertificatePath, "/etc/talos-server-crt/tls.crt", "Path to the Server TLS certificate")
141+
rootCmd.Flags().String(cliTLSPrivateKeyPath, "/etc/talos-server-crt/tls.key", "Path to Server TLS private key")
142+
rootCmd.Flags().String(cliTalosToken, "", "Talos token")
139143
// Bind flags to viper keys
140-
_ = viper.BindPFlag("port", rootCmd.Flags().Lookup("port"))
141-
_ = viper.BindPFlag("ca_cert_path", rootCmd.Flags().Lookup("ca-cert-path"))
142-
_ = viper.BindPFlag("ca_key_path", rootCmd.Flags().Lookup("ca-key-path"))
143-
_ = viper.BindPFlag("tls_cert_path", rootCmd.Flags().Lookup("tls-cert-path"))
144-
_ = viper.BindPFlag("tls_key_path", rootCmd.Flags().Lookup("tls-key-path"))
145-
_ = viper.BindPFlag("talos_token", rootCmd.Flags().Lookup("talos-token"))
144+
_ = viper.BindPFlag(cliPortName, rootCmd.Flags().Lookup(cliPortName))
145+
_ = viper.BindPFlag(cliCACertificatePath, rootCmd.Flags().Lookup(cliCACertificatePath))
146+
_ = viper.BindPFlag(cliCAPrivateKeyPath, rootCmd.Flags().Lookup(cliCAPrivateKeyPath))
147+
_ = viper.BindPFlag(cliTLSCertificatePath, rootCmd.Flags().Lookup(cliTLSCertificatePath))
148+
_ = viper.BindPFlag(cliTLSPrivateKeyPath, rootCmd.Flags().Lookup(cliTLSPrivateKeyPath))
149+
_ = viper.BindPFlag(cliTalosToken, rootCmd.Flags().Lookup(cliTalosToken))
146150
// Allow reading from env variables automatically. Env keys are uppercased and `.` replaced with `_`.
147151
viper.SetEnvPrefix("")
148152
viper.AutomaticEnv()
149153
// Explicit env key mapping (to allow different names if desired)
150-
_ = viper.BindEnv("port", "PORT")
151-
_ = viper.BindEnv("ca_cert_path", "CA_CERT_PATH")
152-
_ = viper.BindEnv("ca_key_path", "CA_KEY_PATH")
153-
_ = viper.BindEnv("tls_cert_path", "TLS_CERT_PATH")
154-
_ = viper.BindEnv("tls_key_path", "TLS_KEY_PATH")
155-
_ = viper.BindEnv("talos_token", "TALOS_TOKEN")
154+
_ = viper.BindEnv(cliPortName, "PORT")
155+
_ = viper.BindEnv(cliCACertificatePath, "CA_CERT_PATH")
156+
_ = viper.BindEnv(cliCAPrivateKeyPath, "CA_KEY_PATH")
157+
_ = viper.BindEnv(cliTLSCertificatePath, "TLS_CERT_PATH")
158+
_ = viper.BindEnv(cliTLSPrivateKeyPath, "TLS_KEY_PATH")
159+
_ = viper.BindEnv(cliTalosToken, "TALOS_TOKEN")
156160

157161
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
158162
defer stop()

0 commit comments

Comments
 (0)