-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathauthenticator.go
More file actions
156 lines (127 loc) · 3.82 KB
/
Copy pathauthenticator.go
File metadata and controls
156 lines (127 loc) · 3.82 KB
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
package jwt
import (
"context"
"crypto/rand"
"crypto/rsa"
"fmt"
"net/http"
"os"
"github.com/cyberark/conjur-authn-k8s-client/pkg/access_token"
"github.com/cyberark/conjur-authn-k8s-client/pkg/authenticator/common"
"github.com/cyberark/conjur-authn-k8s-client/pkg/log"
"github.com/cyberark/conjur-authn-k8s-client/pkg/utils"
"github.com/cyberark/conjur-opentelemetry-tracer/pkg/trace"
)
// Authenticator contains the configuration and client
// for the authentication connection to Conjur
type Authenticator struct {
client *http.Client
privateKey *rsa.PrivateKey
accessToken access_token.AccessToken
Config *Config
}
// NewWithAccessToken creates a new authenticator instance from a given access token
func NewWithAccessToken(config Config, accessToken access_token.AccessToken) (*Authenticator, error) {
signingKey, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
return nil, log.RecordedError(log.CAKC030, err)
}
client, err := common.NewHTTPSClient(config.Common.SSLCertificate, nil, nil)
if err != nil {
return nil, err
}
return &Authenticator{
client: client,
privateKey: signingKey,
accessToken: accessToken,
Config: &config,
}, nil
}
// GetAccessToken is getter for accessToken
func (auth *Authenticator) GetAccessToken() access_token.AccessToken {
return auth.accessToken
}
// Authenticate sends Conjur an authenticate request and writes the response
// to the token file (after decrypting it if needed). It also manages state of
// certificates.
// @deprecated Use AuthenticateWithContext instead
func (auth *Authenticator) Authenticate() error {
ctx, tracer, cleanup, err := trace.Create(
trace.NoopProviderType,
trace.TracerProviderConfig{},
)
if err != nil {
return err
}
defer cleanup(ctx)
return auth.AuthenticateWithContext(ctx, tracer)
}
func (auth *Authenticator) AuthenticateWithContext(ctx context.Context, tr trace.Tracer) error {
log.Info(log.CAKC066)
ctx, span := tr.Start(ctx, "Authenticate")
defer span.End()
authenticationResponse, err := auth.sendAuthenticationRequest(ctx, tr)
if err != nil {
span.RecordErrorAndSetStatus(err)
return err
}
err = auth.accessToken.Write(authenticationResponse)
if err != nil {
span.RecordErrorAndSetStatus(err)
return err
}
log.Info(log.CAKC035)
return nil
}
// sendAuthenticationRequest reads the JWT token from the file system and sends
// an authentication request to the Conjur server. It also validates the response
// code before returning its body
func (auth *Authenticator) sendAuthenticationRequest(ctx context.Context, tracer trace.Tracer) ([]byte, error) {
var authenticatingIdentity string
_, span := tracer.Start(ctx, "Send authentication request")
defer span.End()
jwtToken, err := loadJWTToken(auth.Config.JWTTokenFilePath)
if err != nil {
span.RecordErrorAndSetStatus(err)
return nil, err
}
log.Debug(log.CAKC078)
if auth.Config.Common.Username != nil {
authenticatingIdentity = auth.Config.Common.Username.FullUsername
log.Debug(log.CAKC079, authenticatingIdentity)
} else {
log.Debug(log.CAKC080)
authenticatingIdentity = ""
}
req, err := AuthenticateRequest(
auth.Config.Common.URL,
auth.Config.Common.Account,
authenticatingIdentity,
jwtToken,
)
if err != nil {
span.RecordErrorAndSetStatus(err)
return nil, err
}
log.Debug(log.CAKC069, AuthnType)
resp, err := auth.client.Do(req)
if err != nil {
span.RecordErrorAndSetStatus(err)
return nil, log.RecordedError(log.CAKC027, err)
}
err = utils.ValidateResponse(resp)
if err != nil {
span.RecordErrorAndSetStatus(err)
return nil, err
}
return utils.ReadResponseBody(resp)
}
func loadJWTToken(path string) (string, error) {
log.Debug(log.CAKC076, path)
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf(log.CAKC067, path)
}
log.Debug(log.CAKC077)
return string(data), nil
}