-
Notifications
You must be signed in to change notification settings - Fork 124
/
key.go
182 lines (159 loc) · 5.97 KB
/
key.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Copyright (c) Contributors to the Apptainer project, established as
// Apptainer a Series of LF Projects LLC.
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2020, Control Command Inc. All rights reserved.
// Copyright (c) 2017-2021, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package cli
import (
"errors"
"fmt"
"os"
"github.com/apptainer/apptainer/docs"
"github.com/apptainer/apptainer/internal/pkg/buildcfg"
"github.com/apptainer/apptainer/pkg/cmdline"
"github.com/apptainer/apptainer/pkg/syfs"
"github.com/apptainer/apptainer/pkg/sylog"
"github.com/spf13/cobra"
)
var (
keyServerURI string // -u command line option
keySearchLongList bool // -l option for long-list
keyNewpairBitLength int // -b option for bit length
keyGlobalPubKey bool // -g option to manage global public keys
keyRemovePublic bool //--public option to remove only public keys
keyRemovePrivate bool //--private option to remove only private keys
keyRemoveBoth bool //--both option to remove both public and private keys
keyLocalDir string //--keysdir option for local key dir path
)
// -u|--url
var keyServerURIFlag = cmdline.Flag{
ID: "keyServerURIFlag",
Value: &keyServerURI,
DefaultValue: "",
Name: "url",
ShortHand: "u",
Usage: "specify the key server URL",
EnvKeys: []string{"URL"},
}
// -l|--long-list
var keySearchLongListFlag = cmdline.Flag{
ID: "keySearchLongListFlag",
Value: &keySearchLongList,
DefaultValue: false,
Name: "long-list",
ShortHand: "l",
Usage: "output long list when searching for keys",
}
// -b|--bit-length
var keyNewpairBitLengthFlag = cmdline.Flag{
ID: "keyNewpairBitLengthFlag",
Value: &keyNewpairBitLength,
DefaultValue: 4096,
Name: "bit-length",
ShortHand: "b",
Usage: "specify key bit length",
}
// -g|--global
var keyGlobalPubKeyFlag = cmdline.Flag{
ID: "keyGlobalPubKeyFlag",
Value: &keyGlobalPubKey,
DefaultValue: false,
Name: "global",
ShortHand: "g",
Usage: "manage global public keys (import/pull/remove are restricted to root user or unprivileged installation only)",
}
// --public
var keyRemovePublicKeyFlag = cmdline.Flag{
ID: "keyRemovePublicKeyFlag",
Value: &keyRemovePublic,
DefaultValue: false,
Name: "public",
ShortHand: "p",
Usage: "remove public keys only",
}
// --secret
var keyRemovePrivateKeyFlag = cmdline.Flag{
ID: "keyRemovePrivateKeyFlag",
Value: &keyRemovePrivate,
DefaultValue: false,
Name: "secret",
ShortHand: "s",
Usage: "remove secret keys only",
}
// --both
var keyRemoveBothKeyFlag = cmdline.Flag{
ID: "keyRemoveBothKeyFlag",
Value: &keyRemoveBoth,
DefaultValue: false,
Name: "both",
ShortHand: "b",
Usage: "remove both public and private keys",
}
// --keysdir
var keyLocalDirKeyFlag = cmdline.Flag{
ID: "keyLocalDirKeyFlag",
Value: &keyLocalDir,
DefaultValue: syfs.DefaultLocalKeyDirPath(),
Name: "keysdir",
ShortHand: "d",
Usage: "set local keyring dir path, an alternative way is to set environment variable 'APPTAINER_KEYSDIR'",
}
func init() {
addCmdInit(func(cmdManager *cmdline.CommandManager) {
cmdManager.RegisterCmd(KeyCmd)
cmdManager.RegisterSubCmd(KeyCmd, KeyNewPairCmd)
cmdManager.RegisterFlagForCmd(keyNewPairNameFlag, KeyNewPairCmd)
cmdManager.RegisterFlagForCmd(keyNewPairEmailFlag, KeyNewPairCmd)
cmdManager.RegisterFlagForCmd(keyNewPairCommentFlag, KeyNewPairCmd)
cmdManager.RegisterFlagForCmd(keyNewPairPasswordFlag, KeyNewPairCmd)
cmdManager.RegisterFlagForCmd(keyNewPairPushFlag, KeyNewPairCmd)
cmdManager.RegisterSubCmd(KeyCmd, KeyListCmd)
cmdManager.RegisterSubCmd(KeyCmd, KeySearchCmd)
cmdManager.RegisterSubCmd(KeyCmd, KeyPullCmd)
cmdManager.RegisterSubCmd(KeyCmd, KeyPushCmd)
cmdManager.RegisterSubCmd(KeyCmd, KeyImportCmd)
cmdManager.RegisterSubCmd(KeyCmd, KeyRemoveCmd)
cmdManager.RegisterSubCmd(KeyCmd, KeyExportCmd)
cmdManager.RegisterFlagForCmd(&keyServerURIFlag, KeySearchCmd, KeyPushCmd, KeyPullCmd)
cmdManager.RegisterFlagForCmd(&keySearchLongListFlag, KeySearchCmd)
cmdManager.RegisterFlagForCmd(&keyNewpairBitLengthFlag, KeyNewPairCmd)
cmdManager.RegisterFlagForCmd(&keyImportWithNewPasswordFlag, KeyImportCmd)
cmdManager.SetCmdGroup("key_group_cmd", KeyImportCmd, KeyExportCmd, KeyListCmd, KeyPullCmd, KeyPushCmd, KeyRemoveCmd)
cmdManager.RegisterFlagForCmd(
&keyGlobalPubKeyFlag,
cmdManager.GetCmdGroup("key_group_cmd")...,
)
cmdManager.RegisterFlagForCmd(
&keyLocalDirKeyFlag,
append(cmdManager.GetCmdGroup("key_group_cmd"), KeyNewPairCmd)...,
)
// register public/private/both flags for KeyRemoveCmd only
cmdManager.RegisterFlagForCmd(&keyRemovePublicKeyFlag, KeyRemoveCmd)
cmdManager.RegisterFlagForCmd(&keyRemovePrivateKeyFlag, KeyRemoveCmd)
cmdManager.RegisterFlagForCmd(&keyRemoveBothKeyFlag, KeyRemoveCmd)
})
}
func checkGlobal(cmd *cobra.Command, args []string) {
if !keyGlobalPubKey || os.Geteuid() == 0 || buildcfg.APPTAINER_SUID_INSTALL == 0 {
return
}
path := cmd.CommandPath()
sylog.Fatalf("%q command with --global requires root privileges or an unprivileged installation", path)
}
// KeyCmd is the 'key' command that allows management of keyrings
var KeyCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("invalid command")
},
DisableFlagsInUseLine: true,
Aliases: []string{"keys"},
Use: docs.KeyUse,
Short: docs.KeyShort,
Long: fmt.Sprintf(docs.KeyLong, buildcfg.SYSCONFDIR),
Example: docs.KeyExample,
SilenceErrors: true,
}