-
Notifications
You must be signed in to change notification settings - Fork 18
/
generate_keygen.go
87 lines (76 loc) · 2.78 KB
/
generate_keygen.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
/***************************************************************
*
* Copyright (C) 2024, Pelican Project, Morgridge Institute for Research
*
* Licensed under the Apache License, Version 2.0 (the "License"); you
* may not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************/
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/pelicanplatform/pelican/config"
"github.com/pelicanplatform/pelican/param"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func keygenMain(cmd *cobra.Command, args []string) error {
wd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "failed to get the current working directory")
}
if privateKeyPath == "" {
privateKeyPath = filepath.Join(wd, "issuer.jwk")
} else {
privateKeyPath = filepath.Clean(strings.TrimSpace(privateKeyPath))
}
if err = os.MkdirAll(filepath.Dir(privateKeyPath), 0755); err != nil {
return errors.Wrapf(err, "failed to create directory for private key at %s", filepath.Dir(privateKeyPath))
}
if publicKeyPath == "" {
publicKeyPath = filepath.Join(wd, "issuer-pub.jwks")
} else {
publicKeyPath = filepath.Clean(strings.TrimSpace(publicKeyPath))
}
_, err = os.Stat(privateKeyPath)
if err == nil {
return fmt.Errorf("file exists for private key under %s", privateKeyPath)
}
_, err = os.Stat(publicKeyPath)
if err == nil {
return fmt.Errorf("file exists for public key under %s", publicKeyPath)
}
if err = os.MkdirAll(filepath.Dir(publicKeyPath), 0755); err != nil {
return errors.Wrapf(err, "failed to create directory for public key at %s", filepath.Dir(privateKeyPath))
}
viper.Set(param.IssuerKey.GetName(), privateKeyPath)
// GetIssuerPublicJWKS will generate the private key at IssuerKey if it does not exist
// and parse the private key and generate the corresponding public key for us
pubkey, err := config.GetIssuerPublicJWKS()
if err != nil {
return err
}
bytes, err := json.MarshalIndent(pubkey, "", " ")
if err != nil {
return errors.Wrap(err, "failed to generate json from jwks")
}
if err = os.WriteFile(publicKeyPath, bytes, 0644); err != nil {
return errors.Wrap(err, "fail to write the public key to the file")
}
fmt.Printf("Successfully generated keys at: \nPrivate key: %s\nPublic Key: %s\n", privateKeyPath, publicKeyPath)
return nil
}