-
Notifications
You must be signed in to change notification settings - Fork 124
/
sign.go
147 lines (127 loc) · 4.2 KB
/
sign.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
// 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) 2017-2020, 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 (
"fmt"
"github.com/apptainer/apptainer/docs"
"github.com/apptainer/apptainer/internal/app/apptainer"
"github.com/apptainer/apptainer/pkg/cmdline"
"github.com/apptainer/apptainer/pkg/sylog"
"github.com/apptainer/apptainer/pkg/sypgp"
"github.com/spf13/cobra"
)
var (
privKey int // -k encryption key (index from 'key list --secret') specification
signAll bool
)
// -g|--group-id
var signSifGroupIDFlag = cmdline.Flag{
ID: "signSifGroupIDFlag",
Value: &sifGroupID,
DefaultValue: uint32(0),
Name: "group-id",
ShortHand: "g",
Usage: "sign objects with the specified group ID",
}
// --groupid (deprecated)
var signOldSifGroupIDFlag = cmdline.Flag{
ID: "signOldSifGroupIDFlag",
Value: &sifGroupID,
DefaultValue: uint32(0),
Name: "groupid",
Usage: "sign objects with the specified group ID",
Deprecated: "use '--group-id'",
}
// -i| --sif-id
var signSifDescSifIDFlag = cmdline.Flag{
ID: "signSifDescSifIDFlag",
Value: &sifDescID,
DefaultValue: uint32(0),
Name: "sif-id",
ShortHand: "i",
Usage: "sign object with the specified ID",
}
// --id (deprecated)
var signSifDescIDFlag = cmdline.Flag{
ID: "signSifDescIDFlag",
Value: &sifDescID,
DefaultValue: uint32(0),
Name: "id",
Usage: "sign object with the specified ID",
Deprecated: "use '--sif-id'",
}
// -k|--keyidx
var signKeyIdxFlag = cmdline.Flag{
ID: "signKeyIdxFlag",
Value: &privKey,
DefaultValue: 0,
Name: "keyidx",
ShortHand: "k",
Usage: "private key to use (index from 'key list --secret')",
}
// -a|--all (deprecated)
var signAllFlag = cmdline.Flag{
ID: "signAllFlag",
Value: &signAll,
DefaultValue: false,
Name: "all",
ShortHand: "a",
Usage: "sign all objects",
Deprecated: "now the default behavior",
}
func init() {
addCmdInit(func(cmdManager *cmdline.CommandManager) {
cmdManager.RegisterCmd(SignCmd)
cmdManager.RegisterFlagForCmd(&signSifGroupIDFlag, SignCmd)
cmdManager.RegisterFlagForCmd(&signOldSifGroupIDFlag, SignCmd)
cmdManager.RegisterFlagForCmd(&signSifDescSifIDFlag, SignCmd)
cmdManager.RegisterFlagForCmd(&signSifDescIDFlag, SignCmd)
cmdManager.RegisterFlagForCmd(&signKeyIdxFlag, SignCmd)
cmdManager.RegisterFlagForCmd(&signAllFlag, SignCmd)
})
}
// SignCmd apptainer sign
var SignCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
// args[0] contains image path
doSignCmd(cmd, args[0])
},
Use: docs.SignUse,
Short: docs.SignShort,
Long: docs.SignLong,
Example: docs.SignExample,
}
func doSignCmd(cmd *cobra.Command, cpath string) {
var opts []apptainer.SignOpt
// Set entity selector option, and ensure the entity is decrypted.
var f sypgp.EntitySelector
if cmd.Flag(signKeyIdxFlag.Name).Changed {
f = selectEntityAtIndex(privKey)
} else {
f = selectEntityInteractive()
}
f = decryptSelectedEntityInteractive(f)
opts = append(opts, apptainer.OptSignEntitySelector(f))
// Set group option, if applicable.
if cmd.Flag(signSifGroupIDFlag.Name).Changed || cmd.Flag(signOldSifGroupIDFlag.Name).Changed {
opts = append(opts, apptainer.OptSignGroup(sifGroupID))
}
// Set object option, if applicable.
if cmd.Flag(signSifDescSifIDFlag.Name).Changed || cmd.Flag(signSifDescIDFlag.Name).Changed {
opts = append(opts, apptainer.OptSignObjects(sifDescID))
}
// Sign the image.
fmt.Printf("Signing image: %s\n", cpath)
if err := apptainer.Sign(cpath, opts...); err != nil {
sylog.Fatalf("Failed to sign container: %s", err)
}
fmt.Printf("Signature created and applied to %s\n", cpath)
}