-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublic_key_config.go
104 lines (85 loc) · 2.83 KB
/
public_key_config.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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"errors"
"fmt"
"time"
"github.com/abcxyz/pkg/cli"
)
// PublicKeyConfig is the config used for public key hosting.
type PublicKeyConfig struct {
// ProjectID is the Google Cloud project ID.
ProjectID string `env:"PROJECT_ID"`
// DevMode controls enables more granular debugging in logs.
DevMode bool `env:"DEV_MODE,default=false"`
Port string `env:"PORT,default=8080"`
// KeyNames format: `projects/*/locations/*/keyRings/*/cryptoKeys/*`
// https://pkg.go.dev/google.golang.org/genproto/googleapis/cloud/kms/v1#PublicKeyKey
KeyNames []string `env:"JVS_KEY_NAMES,overwrite"`
CacheTimeout time.Duration `env:"JVS_PUBLIC_KEY_CACHE_TIMEOUT, default=5m"`
}
func (cfg *PublicKeyConfig) Validate() (merr error) {
if cfg.ProjectID == "" {
merr = errors.Join(merr, fmt.Errorf("empty ProjectID"))
}
if len(cfg.KeyNames) == 0 {
merr = errors.Join(merr, fmt.Errorf("empty KeyNames"))
}
if got := cfg.CacheTimeout; got <= 0 {
merr = errors.Join(merr, fmt.Errorf("cache_timeout must be a positive duration, got %q", got))
}
return
}
// ToFlags binds the config to the give [cli.FlagSet] and returns it.
func (cfg *PublicKeyConfig) ToFlags(set *cli.FlagSet) *cli.FlagSet {
// Command options
f := set.NewSection("COMMON SERVER OPTIONS")
f.StringVar(&cli.StringVar{
Name: "project-id",
Target: &cfg.ProjectID,
EnvVar: "PROJECT_ID",
Usage: `Google Cloud project ID.`,
})
f.BoolVar(&cli.BoolVar{
Name: "dev",
Target: &cfg.DevMode,
EnvVar: "DEV_MODE",
Default: false,
Usage: "Set to true to enable more granular debugging in logs",
})
f.StringVar(&cli.StringVar{
Name: "port",
Target: &cfg.Port,
EnvVar: "PORT",
Default: "8080",
Usage: `The port the server listens to.`,
})
f = set.NewSection("KEY OPTIONS")
f.StringSliceVar(&cli.StringSliceVar{
Name: "key-names",
Target: &cfg.KeyNames,
EnvVar: "JVS_KEY_NAMES",
Example: "projects/[JVS_PROJECT]/locations/global/keyRings/[JVS_KEYRING]/cryptoKeys/[JVS_KEY]",
Usage: "List of KMS key names",
})
f.DurationVar(&cli.DurationVar{
Name: "cache-timeout",
Target: &cfg.CacheTimeout,
EnvVar: "JVS_PUBLIC_KEY_CACHE_TIMEOUT",
Default: 5 * time.Minute,
Usage: "The duration that a KMS key will be cached.",
})
return set
}