-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
kuadrant authpolicy command: support apikey #50
Conversation
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## httproute-kuadrant-extensions #50 +/- ##
================================================================
- Coverage 0.38% 0.38% -0.01%
================================================================
Files 16 17 +1
Lines 774 783 +9
================================================================
Hits 3 3
- Misses 771 780 +9 ☔ View full report in Codecov by Sentry. |
👀 |
Works great |
8957a5a
to
546d66f
Compare
@jasonmadigan completed the verification steps and added some doc Ready for review! |
|
||
Like the following example: | ||
|
||
```yaml |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one for later maybe, but do you think it'd be nice to generate an example secret too? not a big deal, just something I thought of while using
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you mean that the command kuadrantctl generate kuadrant authpolicy
generates the authpolicy and additionally some secret? That could be done. Or write in stderr (stdout will likely be used piped with kubectl) a example secret too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exactly yeah - could be handy? not urgent, but nice to have
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sounds good to you?
❯ bin/kuadrantctl generate kuadrant authpolicy --oas examples/oas3/petstore-multiple-sec-requirements.yaml 1>/dev/null
======================================================================================================
POST /v1/cat endpoint is protected with ApiKey. Consider creating secrets with valid tokens
---
apiVersion: v1
kind: Secret
metadata:
creationTimestamp: null
labels:
authorino.kuadrant.io/managed-by: authorino
kuadrant.io/apikeys-by: cat_api_key
name: cat_api_key
stringData:
api_key: MY_SECRET_TOKEN_VALUE
type: Opaque
======================================================================================================
GET /v1/snake endpoint is protected with ApiKey. Consider creating secrets with valid tokens
---
apiVersion: v1
kind: Secret
metadata:
creationTimestamp: null
labels:
authorino.kuadrant.io/managed-by: authorino
kuadrant.io/apikeys-by: snakes_api_key
name: snakes_api_key
stringData:
api_key: MY_SECRET_TOKEN_VALUE
type: Opaque
Note:
stdout
has been redirected to/dev/null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What
The new command
kuadrantctl generate kuadrant authpolicy
to create kuadrant Auth Policy from OpenAPI Specification (OAS) 3.x powered with kuadrant extensions was introduced in #46#46 implemented the Security Scheme Object type
openIdConnect
.This PR implements another type:
apiKey
Example
Running the command
The generated authpolicy
In this particular example, the endpoint
GET /dog
will be protected. The token needs to be in the query string of the request included in a parameter nameddog_token
.Kuadrant will validate received tokens against tokens found in secrets with label
kuadrant.io/apikeys-by: ${sec scheme name}
. In this particular example the label selector will be:kuadrant.io/apikeys-by: securedDog
. Like the following example:For more information about Kuadrant auth based on api key: https://docs.kuadrant.io/authorino/docs/user-guides/api-key-authentication/
Verification Steps
The verification steps will lead you to the process of deploying and testing the following api with endpoints protected using different auth schemes:
GET /api/v1/cat
POST /api/v1/cat
GET /api/v1/dog
GET /api/v1/snake
petstore
petstore
. In the Client Protocol field, selectopenid-connect
.bob
, set the Email Verified switch to ON, and click Save.p
. Enter the password in both the fields, set the Temporary switch to OFF to avoid the password reset at the next login, and clickSet Password
.Now, let's run local cluster to test the kuadrantctl new command to generate authpolicy.
authpolicy-api-key
bin/kuadrantctl
pathPOST /api/v1/cat
endpointGET /api/v1/snake
endpointbin/kuadrantctl generate gatewayapi httproute --oas petstore-openapi.yaml | kubectl apply -n petstore -f -
bin/kuadrantctl generate kuadrant authpolicy --oas petstore-openapi.yaml | kubectl apply -n petstore -f -
Now, we are ready to test OpenAPI endpoints ❗
GET /api/v1/cat
-> It's a public endpoint, hence should return 200 OkPOST /api/v1/cat
-> It's a protected endpoint with apikeyWithout any credentials, it should return
401 Unauthorized
the reason headers tell that
credential not found
. Credentials satisfyingpostCat_cat_api_key
authentication is needed.According to the OpenAPI spec, it should be a header named
api_key
. What if we try a wrong token? one token assigned to other endpoint, i.e.I_LIKE_SNAKES
instead of the valid oneI_LIKE_CATS
. It should return401 Unauthorized
.the reason headers tell that
the API Key provided is invalid
. Using valid token (from the secretcat-api-key-1
assigned toPOST /api/v1/cats
) in theapi_key
header should return 200 OkGET /api/v1/dog
-> It's a protected endpoint with oidc (assigned to our keycloak instance andpetstore
realm)without credentials, it should return
401 Unauthorized
To get the authentication token, this example is using Direct Access Grants oauth2 grant type (also known as Client Credentials grant type). When configuring the Keycloak (OIDC provider) client settings, we enabled Direct Access Grants to enable this procedure. We will be authenticating as
bob
user withp
password. We previously createdbob
user in Keycloak in thepetstore
realm.With the access token in place, let's try to get those puppies
it should return 200 OK
GET /api/v1/snake
-> It's a protected endpoint with oidc (assigned to our keycloak instance andpetstore
realm) OR with apiKeyThis example is to show that multiple sec requirements (with OR semantics) can be specified for an OpenAPI operation.
without credentials, it should return
401 Unauthorized
With the access token in place, it should return 200 OK (unless the token has expired)
With apiKey it should also work. According to the OpenAPI spec security scheme, it should be a query string named
snake_token
and the token needs to be valid token (from the secretsnake-api-key-1
assigned toGET /api/v1/snake
)