-
Notifications
You must be signed in to change notification settings - Fork 20
/
token_generator.go
129 lines (97 loc) · 3.28 KB
/
token_generator.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
/*******************************************************************************
* IBM Confidential
* OCO Source Materials
* IBM Cloud Container Service, 5737-D43
* (C) Copyright IBM Corp. 2018 All Rights Reserved.
* The source code for this program is not published or otherwise divested of
* its trade secrets, irrespective of what has been deposited with
* the U.S. Copyright Office.
******************************************************************************/
package softlayer_file
import (
"crypto/rsa"
"errors"
"io/ioutil"
"path/filepath"
"time"
"github.com/dgrijalva/jwt-go"
"go.uber.org/zap"
"github.com/IBM/ibmcloud-storage-volume-lib/config"
"github.com/IBM/ibmcloud-storage-volume-lib/provider/local"
"github.com/IBM/ibmcloud-storage-volume-lib/lib/provider"
"github.com/IBM/ibmcloud-storage-volume-lib/volume-providers/auth"
)
type tokenGenerator struct {
config *config.SoftlayerConfig
tokenKID string
tokenTTL time.Duration
tokenBeforeTime time.Duration
privateKey *rsa.PrivateKey // Secret. Do not export
}
func (tg *tokenGenerator) readConfig(logger zap.Logger) (err error) {
logger.Debug("Entering readConfig")
defer func() {
logger.Debug("Exiting readConfig", zap.Duration("tokenTTL", tg.tokenTTL), zap.Duration("tokenBeforeTime", tg.tokenBeforeTime), zap.String("tokenKID", tg.tokenKID), local.ZapError(err))
}()
if tg.privateKey != nil {
return
}
tg.tokenBeforeTime = time.Duration(tg.config.SoftlayerJWTValidFrom) * time.Second * -1
tg.tokenTTL = time.Duration(tg.config.SoftlayerJWTTTL) * time.Second
tg.tokenKID = tg.config.SoftlayerJWTKID
path := filepath.Join(config.GetEtcPath(), tg.tokenKID)
pem, err := ioutil.ReadFile(path)
if err != nil {
logger.Error("Error reading PEM", local.ZapError(err))
return
}
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(pem)
if err != nil {
logger.Error("Error parsing PEM", local.ZapError(err))
return
}
tg.privateKey = privateKey
return
}
func (tg *tokenGenerator) buildToken(contextCredentials provider.ContextCredentials, ts time.Time, logger zap.Logger) (token *jwt.Token, err error) {
logger.Debug("Entering getJWTToken", zap.Reflect("contextCredentials", contextCredentials))
defer func() {
logger.Debug("Exiting getJWTToken", zap.Reflect("token", token), local.ZapError(err))
}()
err = tg.readConfig(logger)
if err != nil {
return
}
claims := jwt.MapClaims{
"iss": "armada",
"exp": ts.Add(tg.tokenTTL).Unix(),
"nbf": ts.Add(tg.tokenBeforeTime).Unix(),
"iat": ts.Unix(),
}
switch {
case contextCredentials.UserID == "":
errStr := "User ID is not configured"
logger.Error(errStr)
err = errors.New(errStr)
return
case contextCredentials.AuthType == auth.IMSToken:
claims["ims_user_id"] = contextCredentials.UserID
default:
claims["ims_username"] = contextCredentials.UserID
}
token = jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
token.Header["kid"] = tg.tokenKID
return
}
func (tg *tokenGenerator) getServiceToken(contextCredentials provider.ContextCredentials, logger zap.Logger) (signedToken *string, err error) {
token, err := tg.buildToken(contextCredentials, time.Now(), logger)
if err != nil {
return
}
signedString, err := token.SignedString(tg.privateKey)
if err != nil {
return
}
signedToken = &signedString
return
}