This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
init_secrets.go
151 lines (121 loc) · 4.4 KB
/
init_secrets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package auth
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/flyteorg/flyteadmin/auth/config"
"github.com/flyteorg/flytestdlib/logger"
"github.com/spf13/cobra"
)
const (
SymmetricKeyLength = 32
CookieHashKeyLength = 64
CookieBlockKeyLength = 32
rsaPEMType = "RSA PRIVATE KEY"
)
var (
localPath string
)
// GetInitSecretsCommand creates a command to issue secrets to be used for Auth settings. It writes the secrets to the
// working directory. The expectation is that they are put in a location and made available to the serve command later.
// To configure where the serve command looks for secrets, update this config:
// secrets:
// secrets-prefix: <my custom path>
func GetInitSecretsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Generates secrets needed for OpenIDC and OAuth2 providers",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
secrets, err := NewSecrets()
if err != nil {
return err
}
d, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to get working directory. Error: %w", err)
}
// If not overridden in cmd flags, use the working directory
if len(localPath) == 0 {
localPath = d
}
err = os.MkdirAll(localPath, 0755)
if err != nil {
return fmt.Errorf("failed to create path [%v]. Error: %w", localPath, err)
}
return writeSecrets(ctx, secrets, localPath)
},
}
cmd.Flags().StringVarP(&localPath, "localPath", "p", "", "Specifies where the secrets should be written.")
return cmd
}
type SecretsSet struct {
TokenHashKey []byte
TokenSigningRSAPrivateKey *rsa.PrivateKey
CookieHashKey []byte
CookieBlockKey []byte
}
func writeSecrets(ctx context.Context, secrets SecretsSet, path string) error {
err := ioutil.WriteFile(filepath.Join(path, config.SecretNameClaimSymmetricKey), []byte(base64.RawStdEncoding.EncodeToString(secrets.TokenHashKey)), os.ModePerm)
if err != nil {
return fmt.Errorf("failed to persist token hash key. Error: %w", err)
}
logger.Infof(ctx, "wrote %v", config.SecretNameClaimSymmetricKey)
err = ioutil.WriteFile(filepath.Join(path, config.SecretNameCookieHashKey), []byte(base64.RawStdEncoding.EncodeToString(secrets.CookieHashKey)), os.ModePerm)
if err != nil {
return fmt.Errorf("failed to persist cookie hash key. Error: %w", err)
}
logger.Infof(ctx, "wrote %v", config.SecretNameCookieHashKey)
err = ioutil.WriteFile(filepath.Join(path, config.SecretNameCookieBlockKey), []byte(base64.RawStdEncoding.EncodeToString(secrets.CookieBlockKey)), os.ModePerm)
if err != nil {
return fmt.Errorf("failed to persist cookie block key. Error: %w", err)
}
logger.Infof(ctx, "wrote %v", config.SecretNameCookieBlockKey)
keyOut, err := os.OpenFile(filepath.Join(path, config.SecretNameTokenSigningRSAKey), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to open key.pem for writing: %w", err)
}
privBytes := x509.MarshalPKCS1PrivateKey(secrets.TokenSigningRSAPrivateKey)
if err := pem.Encode(keyOut, &pem.Block{Type: rsaPEMType, Bytes: privBytes}); err != nil {
return fmt.Errorf("failed to write data to key.pem: %w", err)
}
if err := keyOut.Close(); err != nil {
return fmt.Errorf("error closing key.pem: %w", err)
}
logger.Infof(ctx, "wrote %v", config.SecretNameTokenSigningRSAKey)
return nil
}
func NewSecrets() (SecretsSet, error) {
secret := make([]byte, SymmetricKeyLength)
_, err := rand.Read(secret)
if err != nil {
return SecretsSet{}, fmt.Errorf("failed to issue token hash key. Error: %w", err)
}
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return SecretsSet{}, fmt.Errorf("failed to issue token signing key. Error: %w", err)
}
cookieHashKey := make([]byte, CookieHashKeyLength)
_, err = rand.Read(cookieHashKey)
if err != nil {
return SecretsSet{}, fmt.Errorf("failed to issue cookie hash key. Error: %w", err)
}
cookieBlockKey := make([]byte, CookieBlockKeyLength)
_, err = rand.Read(cookieBlockKey)
if err != nil {
return SecretsSet{}, fmt.Errorf("failed to issue cookie block key. Error: %w", err)
}
return SecretsSet{
TokenHashKey: secret,
TokenSigningRSAPrivateKey: privateKey,
CookieHashKey: cookieHashKey,
CookieBlockKey: cookieBlockKey,
}, nil
}