Skip to content

Commit 0ee47ec

Browse files
committed
add SetAuthentication function to authenticate 'passively' (without sending a self-check request to the server)
Signed-off-by: Tim Ramlot <[email protected]>
1 parent c58963f commit 0ee47ec

File tree

6 files changed

+68
-42
lines changed

6 files changed

+68
-42
lines changed

pkg/endpoint/endpoint.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ type Connector interface {
8484
// SetHTTPClient allows to set custom http.Client to this Connector.
8585
SetHTTPClient(client *http.Client)
8686
Ping() (err error)
87+
// SetAuthentication can be used to set the authentication details for the connector, it does not perform the validation
88+
// done by Authenticate. It is useful when you want to set the authentication details without validating them.
89+
SetAuthentication(auth *Authentication) (err error)
90+
// Authenticate calls SetAuthentication and then validates the authentication details by making a request to the server.
8791
// Authenticate is usually called by NewClient and it is not required that you manually call it.
8892
Authenticate(auth *Authentication) (err error)
8993

pkg/venafi/cloud/cloud.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func (c *Connector) getHTTPClient() *http.Client {
312312
}
313313

314314
func (c *Connector) request(method string, url string, data interface{}, authNotRequired ...bool) (statusCode int, statusText string, body []byte, err error) {
315-
if (c.accessToken == "" && c.user == nil) || (c.user != nil && c.user.Company == nil) {
315+
if c.accessToken == "" && c.apiKey == "" {
316316
if !(len(authNotRequired) == 1 && authNotRequired[0]) {
317317
err = fmt.Errorf("%w: must be autheticated to make requests to TLSPC API", verror.VcertError)
318318
return

pkg/venafi/cloud/connector.go

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ type Connector struct {
9090
apiKey string
9191
accessToken string
9292
verbose bool
93-
user *userDetails
9493
trust *x509.CertPool
9594
zone cloudZone
9695
client *http.Client
@@ -134,17 +133,42 @@ func (c *Connector) Ping() (err error) {
134133
return nil
135134
}
136135

137-
// Authenticate authenticates the user with Venafi Cloud using the provided API Key
136+
// Authenticate sets the authentication credentials for the Venafi Cloud API.
137+
// It will send a request to the API to verify the credentials are correct.
138138
func (c *Connector) Authenticate(auth *endpoint.Authentication) error {
139+
if err := c.SetAuthentication(auth); err != nil {
140+
return err
141+
}
142+
143+
url := c.getURL(urlResourceUserAccounts)
144+
statusCode, status, body, err := c.request("GET", url, nil, true)
145+
if err != nil {
146+
return fmt.Errorf("%w: %s", verror.AuthError, err)
147+
}
148+
if _, err := parseUserDetailsResult(http.StatusOK, statusCode, status, body); err != nil {
149+
return fmt.Errorf("%w: %s", verror.AuthError, err)
150+
}
151+
152+
return nil
153+
}
154+
155+
// SetAuthentication sets the authentication credentials for the Venafi Cloud API.
156+
func (c *Connector) SetAuthentication(auth *endpoint.Authentication) (err error) {
157+
defer func() {
158+
if err != nil {
159+
err = fmt.Errorf("%w: %s", verror.AuthError, err)
160+
}
161+
}()
162+
139163
if auth == nil {
140164
return fmt.Errorf("failed to authenticate: missing credentials")
141165
}
142166

143-
//1. Access token. Assign it to connector
144167
if auth.AccessToken != "" {
168+
// 1. Access token. Assign it to connector
145169
c.accessToken = auth.AccessToken
146170
} else if auth.TokenURL != "" && auth.ExternalJWT != "" {
147-
//2. JWT and token URL. use it to request new access token
171+
// 2. JWT and token URL. use it to request new access token
148172
tokenResponse, err := c.GetAccessToken(auth)
149173
if err != nil {
150174
return err
@@ -153,16 +177,6 @@ func (c *Connector) Authenticate(auth *endpoint.Authentication) error {
153177
} else if auth.APIKey != "" {
154178
// 3. API key. Get user to test authentication
155179
c.apiKey = auth.APIKey
156-
url := c.getURL(urlResourceUserAccounts)
157-
statusCode, status, body, err := c.request("GET", url, nil, true)
158-
if err != nil {
159-
return err
160-
}
161-
ud, err := parseUserDetailsResult(http.StatusOK, statusCode, status, body)
162-
if err != nil {
163-
return err
164-
}
165-
c.user = ud
166180
}
167181

168182
// Initialize clients
@@ -947,7 +961,7 @@ func (c *Connector) isAuthenticated() bool {
947961
return true
948962
}
949963

950-
if c.user != nil && c.user.Company != nil {
964+
if c.apiKey != "" {
951965
return true
952966
}
953967

@@ -1430,12 +1444,10 @@ func (c *Connector) CreateUserAccount(userAccount *userAccount) (int, *userDetai
14301444
if err != nil {
14311445
return statusCode, nil, err
14321446
}
1433-
//c.user = ud
14341447
return statusCode, ud, nil
14351448
}
14361449

14371450
func (c *Connector) getUserDetails() (*userDetails, error) {
1438-
14391451
url := c.getURL(urlResourceUserAccounts)
14401452
statusCode, status, body, err := c.request("GET", url, nil)
14411453
if err != nil {
@@ -1445,7 +1457,6 @@ func (c *Connector) getUserDetails() (*userDetails, error) {
14451457
if err != nil {
14461458
return nil, err
14471459
}
1448-
c.user = ud
14491460
return ud, nil
14501461
}
14511462

pkg/venafi/fake/connector.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func (c *Connector) Authenticate(auth *endpoint.Authentication) (err error) {
176176
return
177177
}
178178

179+
func (c *Connector) SetAuthentication(auth *endpoint.Authentication) (err error) {
180+
return
181+
}
182+
179183
type fakeRequestID struct {
180184
Req *certificate.Request
181185
CSR string

pkg/venafi/firefly/connector.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,20 @@ func (c *Connector) GetType() endpoint.ConnectorType {
8585
return endpoint.ConnectorTypeFirefly
8686
}
8787

88+
// Authenticate authenticates the connector to the Firefly server.
89+
// In the future, this method will send a request to the Firefly server to validate the authentication.
8890
func (c *Connector) Authenticate(auth *endpoint.Authentication) error {
91+
if err := c.SetAuthentication(auth); err != nil {
92+
return err
93+
}
94+
95+
// TODO: use the access token to send a request and validate the authentication.
96+
97+
return nil
98+
}
99+
100+
// SetAuthentication sets the authentication details to connect to the Firefly server
101+
func (c *Connector) SetAuthentication(auth *endpoint.Authentication) error {
89102
if auth == nil {
90103
msg := "failed to authenticate: no credentials provided"
91104
zap.L().Error(msg, fieldPlatform)

pkg/venafi/tpp/connector.go

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ type Connector struct {
4545
apiKey string
4646
accessToken string
4747
verbose bool
48-
Identity identity
4948
trust *x509.CertPool
5049
zone string
5150
client *http.Client
@@ -118,8 +117,22 @@ func (c *Connector) Ping() (err error) {
118117
return
119118
}
120119

121-
// Authenticate authenticates the user to the TPP
122-
func (c *Connector) Authenticate(auth *endpoint.Authentication) (err error) {
120+
// Authenticate sets the Authentication details for the TPP Server and
121+
// verifies that it can retrieve Self Identity.
122+
func (c *Connector) Authenticate(auth *endpoint.Authentication) error {
123+
if err := c.SetAuthentication(auth); err != nil {
124+
return err
125+
}
126+
127+
if _, err := c.retrieveSelfIdentity(); err != nil {
128+
return fmt.Errorf("%w: %s", verror.AuthError, err)
129+
}
130+
131+
return nil
132+
}
133+
134+
// SetAuthentication sets the Authentication details for the TPP Server.
135+
func (c *Connector) SetAuthentication(auth *endpoint.Authentication) (err error) {
123136
defer func() {
124137
if err != nil {
125138
err = fmt.Errorf("%w: %s", verror.AuthError, err)
@@ -143,13 +156,6 @@ func (c *Connector) Authenticate(auth *endpoint.Authentication) (err error) {
143156

144157
resp := result.(authorizeResponse)
145158
c.apiKey = resp.APIKey
146-
147-
if c.client != nil {
148-
c.Identity, err = c.retrieveSelfIdentity()
149-
if err != nil {
150-
return err
151-
}
152-
}
153159
return nil
154160

155161
} else if auth.RefreshToken != "" {
@@ -161,24 +167,12 @@ func (c *Connector) Authenticate(auth *endpoint.Authentication) (err error) {
161167

162168
resp := result.(OauthRefreshAccessTokenResponse)
163169
c.accessToken = resp.Access_token
170+
auth.AccessToken = resp.Access_token
164171
auth.RefreshToken = resp.Refresh_token
165-
if c.client != nil {
166-
c.Identity, err = c.retrieveSelfIdentity()
167-
if err != nil {
168-
return err
169-
}
170-
}
171172
return nil
172173

173174
} else if auth.AccessToken != "" {
174175
c.accessToken = auth.AccessToken
175-
176-
if c.client != nil {
177-
c.Identity, err = c.retrieveSelfIdentity()
178-
if err != nil {
179-
return err
180-
}
181-
}
182176
return nil
183177
}
184178
return fmt.Errorf("failed to authenticate: can't determine valid credentials set")

0 commit comments

Comments
 (0)