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