-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (136 loc) · 4.72 KB
/
main.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
package main
import (
"errors"
"fmt"
"log/slog"
"os"
"github.com/snapcrafters/tokenator/internal/config"
"github.com/snapcrafters/tokenator/internal/tokenator"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
version string = "dev"
commit string = "dev"
repositories []string
verbose bool
)
var shortDesc = "A utility for distributing credentials to Snapcrafters repositories."
var longDesc string = `A utility for distributing credentials to Snapcrafters repositories.
tokenator provides automation for:
- Generating Snap store tokens for publishing and promotion of snaps
- Generating Github Personal Access Tokens
- Accepting Github Personal Access Token requests against an org
- Setting secrets across different Github environments
This tool is configured using a single file in one of the three following locations:
- ./tokenator.yaml
- $HOME/.config/tokenator/tokenator.yaml
- /etc/tokenator/tokenator.yaml
For more details on the configuration format, see the homepage below.
The following environment variables must be set:
- TOKENATOR_SNAPCRAFTERS_ORG_PAT - Github Personal Access Token with Snapcrafters org privileges
- TOKENATOR_SNAPCRAFT_LOGIN - Snap Store login
- TOKENATOR_SNAPCRAFT_PASSWORD - Snap Store password
- TOKENATOR_LP_AUTH - Launchpad Remote Build auth file contents
- TOKENATOR_SNAPCRAFTERS_BOT_LOGIN - Github login for the "snapcrafters-bot" user
- TOKENATOR_SNAPCRAFTERS_BOT_PASSWORD - Github password for the "snapcrafters-bot" user
- TOKENATOR_SNAPCRAFTERS_BOT_TOTP_SECRET - Github TOTP secret for "snapcrafters-bot" user
- TOKENATOR_APP_ID - ID of the Github app
- TOKENATOR_APP_SECRET - Client secret for the Github app
For more information, visit the homepage at: https://github.com/snapcrafters/tokenator
`
var rootCmd = &cobra.Command{
Use: "tokenator",
Version: fmt.Sprintf("%s (%s)", version, commit),
Short: shortDesc,
Long: longDesc,
SilenceErrors: false,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
tokenator.SetupLogger(verbose)
cfg, err := parseConfig()
if err != nil {
return fmt.Errorf("failed to parse config: %w", err)
}
creds, err := parseCreds()
if err != nil {
return fmt.Errorf("failed to parse credentials: %w", err)
}
mgr := tokenator.NewManager(*cfg, creds)
err = mgr.Process(repositories)
if err != nil {
slog.Error(err.Error())
}
return nil
},
}
func main() {
// Set the default config file name/type.
viper.SetConfigName("tokenator")
viper.SetConfigType("yaml")
// Add some default config paths.
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.config/tokenator")
viper.AddConfigPath("/etc/tokenator")
// Setup environment variable parsing.
viper.SetEnvPrefix("TOKENATOR")
rootCmd.Flags().StringSliceVarP(&repositories, "repos", "r", []string{}, "comma-separated subset of repos to process. If omitted all configured repos will be processed.")
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose logging")
err := rootCmd.Execute()
if err != nil {
slog.Error(err.Error())
os.Exit(1)
}
}
// parseCreds ensures that all required credentials are set and returns them
// in a format that can be passed to the manager.
func parseCreds() (config.Credentials, error) {
requiredCredentials := []string{
"snapcraft_login",
"snapcraft_password",
"snapcrafters_org_pat",
"snapcrafters_bot_login",
"snapcrafters_bot_password",
"snapcrafters_bot_totp_secret",
"app_id",
"app_secret",
"lp_auth",
}
for _, cred := range requiredCredentials {
viper.MustBindEnv(cred)
}
creds := config.Credentials{
GithubToken: viper.GetString("snapcrafters_org_pat"),
Launchpad: viper.GetString("lp_auth"),
SnapStore: config.LoginCredentials{
Login: viper.GetString("snapcraft_login"),
Password: viper.GetString("snapcraft_password"),
},
Bot: config.LoginCredentials{
Login: viper.GetString("snapcrafters_bot_login"),
Password: viper.GetString("snapcrafters_bot_password"),
TOTPSecret: viper.GetString("snapcrafters_bot_totp_secret"),
},
GithubApp: config.GithubAppCredentials{
ID: viper.GetInt("app_id"),
Secret: viper.GetString("app_secret"),
},
}
return creds, nil
}
// parseConfig reads in the config and parses it into the correct format
func parseConfig() (*config.Config, error) {
err := viper.ReadInConfig()
if err != nil {
if errors.As(err, &viper.ConfigFileNotFoundError{}) {
return nil, errors.New("no config file found, see 'tokenator --help' for details")
}
return nil, errors.New("error parsing tokenator config file")
}
conf := &config.Config{}
err = viper.Unmarshal(conf)
if err != nil {
return nil, errors.New("error parsing tokenator config file")
}
return conf, nil
}