Skip to content

Commit

Permalink
Test authz webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
tsipinakis committed Aug 11, 2023
1 parent 979edd3 commit ef14170
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/auth/webhook_client_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ func (client *webhookClient) Authorize(
client.logger.Debug(err)
return &webhookClientContext{meta.AuthFailed(), false, err}
}
return client.processAuthzWithRetry(meta)

url := client.endpoint + "/authz"
authzRequest := auth.AuthorizationRequest{
ConnectionAuthenticatedMetadata: meta,
}

return client.processAuthzWithRetry(meta, url, authzRequest)
}

func (client *webhookClient) Password(
Expand Down Expand Up @@ -268,10 +274,9 @@ func (client *webhookClient) authServerRequest(endpoint string, requestObject in

func (client *webhookClient) processAuthzWithRetry(
meta metadata.ConnectionAuthenticatedMetadata,
url string,
authzRequest interface{},
) AuthenticationContext {
url := client.endpoint + "/authz"
authzRequest := auth.AuthorizationRequest{}

ctx, cancel := context.WithTimeout(context.Background(), client.timeout)
defer cancel()
var lastError error
Expand Down
31 changes: 31 additions & 0 deletions internal/auth/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ func (h *handler) OnAuthorization(meta metadata.ConnectionAuthenticatedMetadata)
metadata.ConnectionAuthenticatedMetadata,
error,
) {
if meta.RemoteAddress.IP.String() != "127.0.0.1" {
return false, meta.AuthFailed(), fmt.Errorf("invalid IP: %s", meta.RemoteAddress.IP.String())
}
if meta.ConnectionID != "0123456789ABCDEF" {
return false, meta.AuthFailed(), fmt.Errorf("invalid connection ID: %s", meta.ConnectionID)
}
if meta.AuthenticatedUsername == "foo" {
return true, meta.AuthFailed(), nil
}
if meta.Username == "crash" {
// Simulate a database failure
return false, meta.AuthFailed(), fmt.Errorf("database error")
}
return false, meta.AuthFailed(), nil
}

Expand Down Expand Up @@ -143,6 +156,24 @@ func TestAuth(t *testing.T) {
)
assert.NotEqual(t, nil, authenticationContext.Error())
assert.Equal(t, false, authenticationContext.Success())

authenticationContext = client.Authorize(
metadata.NewTestAuthenticatingMetadata("foo").Authenticated("foo"),
)
assert.Equal(t, nil, authenticationContext.Error())
assert.Equal(t, true, authenticationContext.Success())

authenticationContext = client.Authorize(
metadata.NewTestAuthenticatingMetadata("foo").Authenticated("foonoauthz"),
)
assert.Equal(t, nil, authenticationContext.Error())
assert.Equal(t, false, authenticationContext.Success())

authenticationContext = client.Authorize(
metadata.NewTestAuthenticatingMetadata("crash").Authenticated("crash"),
)
assert.NotEqual(t, nil, authenticationContext.Error())
assert.Equal(t, false, authenticationContext.Success())
},
)
}
Expand Down

0 comments on commit ef14170

Please sign in to comment.