blob: fe2423e870be906bbd05d40a06dd1d7bf0702362 [file] [log] [blame]
Jan Schära9b060b2024-08-07 10:42:29 +02001// Package flagdefs contains shared flag definitions for Metropolis.
2// The usage is the same as for the standard flags in the [flag] package,
3// except that the [flag.FlagSet] needs to be passed in the first parameter.
4// Pass [flag.CommandLine] to use the default FlagSet.
5// There are also separate functions for use with the [pflag] package.
6package flagdefs
7
8import (
9 "errors"
10 "flag"
11 "strings"
12
13 "github.com/spf13/pflag"
14
15 cpb "source.monogon.dev/metropolis/proto/common"
16)
17
18// tpmModeValue implements the [flag.Value] and [pflag.Value] interfaces.
19type tpmModeValue cpb.ClusterConfiguration_TPMMode
20
21func (v *tpmModeValue) Set(val string) error {
22 var tpmMode cpb.ClusterConfiguration_TPMMode
23 switch strings.ToLower(val) {
24 case "required", "require":
25 tpmMode = cpb.ClusterConfiguration_TPM_MODE_REQUIRED
26 case "best-effort", "besteffort":
27 tpmMode = cpb.ClusterConfiguration_TPM_MODE_BEST_EFFORT
28 case "disabled", "disable":
29 tpmMode = cpb.ClusterConfiguration_TPM_MODE_DISABLED
30 default:
31 return errors.New("must be one of: required, best-effort, disabled")
32 }
33 *v = tpmModeValue(tpmMode)
34 return nil
35}
36
37func (v *tpmModeValue) String() string {
38 switch cpb.ClusterConfiguration_TPMMode(*v) {
39 case cpb.ClusterConfiguration_TPM_MODE_REQUIRED:
40 return "required"
41 case cpb.ClusterConfiguration_TPM_MODE_BEST_EFFORT:
42 return "best-effort"
43 case cpb.ClusterConfiguration_TPM_MODE_DISABLED:
44 return "disabled"
45 default:
46 return ""
47 }
48}
49
50func (*tpmModeValue) Type() string {
51 return "tpmMode"
52}
53
54// TPMModeVar defines a TPMMode flag with specified name, default value, and
55// usage string. The argument p points to a TPMMode variable in which to store
56// the value of the flag.
57func TPMModeVar(flags *flag.FlagSet, p *cpb.ClusterConfiguration_TPMMode, name string, value cpb.ClusterConfiguration_TPMMode, usage string) {
58 *p = value
59 flags.Var((*tpmModeValue)(p), name, usage+" (one of: required, best-effort, disabled)")
60}
61
62// TPMMode defines a TPMMode flag with specified name, default value, and
63// usage string. The return value is the address of a TPMMode variable that
64// stores the value of the flag.
65func TPMMode(flags *flag.FlagSet, name string, value cpb.ClusterConfiguration_TPMMode, usage string) *cpb.ClusterConfiguration_TPMMode {
66 val := new(cpb.ClusterConfiguration_TPMMode)
67 TPMModeVar(flags, val, name, value, usage)
68 return val
69}
70
71// TPMModeVarPflag defines a TPMMode flag with specified name, default value,
72// and usage string. The argument p points to a TPMMode variable in which to
73// store the value of the flag.
74func TPMModeVarPflag(flags *pflag.FlagSet, p *cpb.ClusterConfiguration_TPMMode, name string, value cpb.ClusterConfiguration_TPMMode, usage string) {
75 *p = value
76 flags.Var((*tpmModeValue)(p), name, usage+" (one of: required, best-effort, disabled)")
77}
78
79// TPMModePflag defines a TPMMode flag with specified name, default value, and
80// usage string. The return value is the address of a TPMMode variable that
81// stores the value of the flag.
82func TPMModePflag(flags *pflag.FlagSet, name string, value cpb.ClusterConfiguration_TPMMode, usage string) *cpb.ClusterConfiguration_TPMMode {
83 val := new(cpb.ClusterConfiguration_TPMMode)
84 TPMModeVarPflag(flags, val, name, value, usage)
85 return val
86}
87
88// storageSecurityPolicyValue implements the [flag.Value] and [pflag.Value]
89// interfaces.
90type storageSecurityPolicyValue cpb.ClusterConfiguration_StorageSecurityPolicy
91
92func (v *storageSecurityPolicyValue) Set(val string) error {
93 var storageSecurityPolicy cpb.ClusterConfiguration_StorageSecurityPolicy
94 switch strings.ToLower(val) {
95 case "permissive":
96 storageSecurityPolicy = cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_PERMISSIVE
97 case "needs-encryption":
98 storageSecurityPolicy = cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_NEEDS_ENCRYPTION
99 case "needs-encryption-and-authentication":
100 storageSecurityPolicy = cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_NEEDS_ENCRYPTION_AND_AUTHENTICATION
101 case "needs-insecure":
102 storageSecurityPolicy = cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_NEEDS_INSECURE
103 default:
104 return errors.New("must be one of: permissive, needs-encryption, needs-encryption-and-authentication, needs-insecure")
105 }
106 *v = storageSecurityPolicyValue(storageSecurityPolicy)
107 return nil
108}
109
110func (v *storageSecurityPolicyValue) String() string {
111 switch cpb.ClusterConfiguration_StorageSecurityPolicy(*v) {
112 case cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_PERMISSIVE:
113 return "permissive"
114 case cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_NEEDS_ENCRYPTION:
115 return "needs-encryption"
116 case cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_NEEDS_ENCRYPTION_AND_AUTHENTICATION:
117 return "needs-encryption-and-authentication"
118 case cpb.ClusterConfiguration_STORAGE_SECURITY_POLICY_NEEDS_INSECURE:
119 return "needs-insecure"
120 default:
121 return ""
122 }
123}
124
125func (*storageSecurityPolicyValue) Type() string {
126 return "storageSecurityPolicy"
127}
128
129// StorageSecurityPolicyVar defines a StorageSecurityPolicy flag with specified
130// name, default value, and usage string. The argument p points to a
131// StorageSecurityPolicy variable in which to store the value of the flag.
132func StorageSecurityPolicyVar(flags *flag.FlagSet, p *cpb.ClusterConfiguration_StorageSecurityPolicy, name string, value cpb.ClusterConfiguration_StorageSecurityPolicy, usage string) {
133 *p = value
134 flags.Var((*storageSecurityPolicyValue)(p), name, usage+" (one of: permissive, needs-encryption, needs-encryption-and-authentication, needs-insecure)")
135}
136
137// StorageSecurityPolicy defines a StorageSecurityPolicy flag with specified
138// name, default value, and usage string. The return value is the address of a
139// StorageSecurityPolicy variable that stores the value of the flag.
140func StorageSecurityPolicy(flags *flag.FlagSet, name string, value cpb.ClusterConfiguration_StorageSecurityPolicy, usage string) *cpb.ClusterConfiguration_StorageSecurityPolicy {
141 val := new(cpb.ClusterConfiguration_StorageSecurityPolicy)
142 StorageSecurityPolicyVar(flags, val, name, value, usage)
143 return val
144}
145
146// StorageSecurityPolicyVarPflag defines a StorageSecurityPolicy flag with
147// specified name, default value, and usage string. The argument p points to a
148// StorageSecurityPolicy variable in which to store the value of the flag.
149func StorageSecurityPolicyVarPflag(flags *pflag.FlagSet, p *cpb.ClusterConfiguration_StorageSecurityPolicy, name string, value cpb.ClusterConfiguration_StorageSecurityPolicy, usage string) {
150 *p = value
151 flags.Var((*storageSecurityPolicyValue)(p), name, usage+" (one of: permissive, needs-encryption, needs-encryption-and-authentication, needs-insecure)")
152}
153
154// StorageSecurityPolicyPflag defines a StorageSecurityPolicy flag with
155// specified name, default value, and usage string. The return value is the
156// address of a StorageSecurityPolicy variable that stores the value of the
157// flag.
158func StorageSecurityPolicyPflag(flags *pflag.FlagSet, name string, value cpb.ClusterConfiguration_StorageSecurityPolicy, usage string) *cpb.ClusterConfiguration_StorageSecurityPolicy {
159 val := new(cpb.ClusterConfiguration_StorageSecurityPolicy)
160 StorageSecurityPolicyVarPflag(flags, val, name, value, usage)
161 return val
162}