-
Notifications
You must be signed in to change notification settings - Fork 106
feat(identity): authenticate principals with Basic Auth #5451
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
Open
juliamrch
wants to merge
23
commits into
feat/principal-directory-reference
Choose a base branch
from
iss5037
base: feat/principal-directory-reference
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e9c14a2
feat(identity): duplicate guide
juliamrch 3ce4f87
feat(identity): write guide
juliamrch 0aa3fe6
First draft of principals reference, it's rough
cloudjumpercat 2800d1a
revise
cloudjumpercat a950917
fix the failing build
cloudjumpercat 1a15fc4
Fixes from spec, other wording and formatting, add API request examples
cloudjumpercat f14c9bd
Kong Identity variable
cloudjumpercat 329dace
feat(identity): add links + fix syntax
juliamrch e977c0f
Potential fix for pull request finding
juliamrch 3a08f20
Potential fix for pull request finding
juliamrch 9acd4fb
Potential fix for pull request finding
juliamrch afd5aff
Potential fix for pull request finding
juliamrch af254d0
Potential fix for pull request finding
juliamrch 87f9df9
feat(identity): add to index
juliamrch ac9d028
Merge branch 'release/kong-identity-m0' into iss5037
juliamrch b395792
styles: add datetimes to dictionary
juliamrch 9640269
Merge branch 'feat/principal-directory-reference' into iss5037
juliamrch 1b1e4d6
Merge branch 'feat/principal-directory-reference' into iss5037
juliamrch 5036bf9
fix(identity): path to kong identity.svg
juliamrch 7401339
feat(identity): create a principal step template
juliamrch f2344a8
feat(identity): include password endpoint
juliamrch bd889a7
feat(identity): add directory name template
juliamrch a6d9a0b
fix(identity): variable export
juliamrch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
174 changes: 174 additions & 0 deletions
174
app/_how-tos/gateway/authenticate-principals-with-basic-authentication.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,174 @@ | ||||||
| --- | ||||||
| title: Authenticate Principals with basic authentication | ||||||
| permalink: /how-to/authenticate-principals-with-basic-authentication/ | ||||||
| content_type: how_to | ||||||
| related_resources: | ||||||
| - text: Authentication | ||||||
| url: /gateway/authentication/ | ||||||
|
|
||||||
| description: Use the Basic Authentication plugin to allow Principals to authenticate with a username and password. | ||||||
| products: | ||||||
| - gateway | ||||||
|
|
||||||
| plugins: | ||||||
| - basic-auth | ||||||
|
|
||||||
| works_on: | ||||||
| - on-prem | ||||||
| - konnect | ||||||
|
|
||||||
| min_version: | ||||||
| gateway: '3.4' | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| entities: | ||||||
| - plugin | ||||||
| - service | ||||||
| - route | ||||||
| - principal | ||||||
|
|
||||||
| tags: | ||||||
| - authentication | ||||||
|
|
||||||
| tldr: | ||||||
| q: How do I authenticate Principals with basic authentication? | ||||||
| a: | | ||||||
| Create a Principal and enable the Basic Authentication plugin globally with `principals.enabled: true`. Set `principals.directory` to your directory ID, then authenticate with the base64-encoded Principal credentials. | ||||||
| tools: | ||||||
| - deck | ||||||
|
|
||||||
| prereqs: | ||||||
| entities: | ||||||
| services: | ||||||
| - example-service | ||||||
| routes: | ||||||
| - example-route | ||||||
|
|
||||||
| cleanup: | ||||||
| inline: | ||||||
| - title: Clean up Konnect environment | ||||||
| include_content: cleanup/platform/konnect | ||||||
| icon_url: /assets/icons/gateway.svg | ||||||
| - title: Destroy the {{site.base_gateway}} container | ||||||
| include_content: cleanup/products/gateway | ||||||
| icon_url: /assets/icons/gateway.svg | ||||||
| --- | ||||||
|
|
||||||
| ## Create a Directory | ||||||
|
|
||||||
| [Directories](/identity/principals/) let you assign a list of [Principals](/identity/principals/) to authenticate clients interacting with {{site.base_gateway}} services. | ||||||
|
|
||||||
| To start using Principals, create a Directory by sending a `POST` request to the [`/v2/directories` endpoint](/api/konnect/kong-identity/v2/#/operations/createDirectory). This creates a Directory named `example-directory`: | ||||||
|
|
||||||
|
|
||||||
| ```bash | ||||||
| curl -X POST "https://us.api.konghq.com/v2/directories" \ | ||||||
| --no-progress-meter --fail-with-body \ | ||||||
| -H "Authorization: Bearer $KONNECT_TOKEN" \ | ||||||
| --json '{ | ||||||
| "name": "example-directory", | ||||||
| "description": "Example directory for principals", | ||||||
| "allow_all_control_planes": true | ||||||
| }' | ||||||
| ``` | ||||||
|
|
||||||
| Save the returned ID as `$DIRECTORY_ID`: | ||||||
|
|
||||||
|
|
||||||
| ```bash | ||||||
| export DIRECTORY_ID='DIRECTORY_ID' | ||||||
| ``` | ||||||
|
|
||||||
|
|
||||||
| ## Create a Principal | ||||||
|
|
||||||
| Create the Principal by sending a POST request to the `/v2/directories/{directoryId}/principals` [endpoint](/api/konnect/kong-identity/v2/#/operations/createPrincipal). This creates a Principal named `example-principal`: | ||||||
|
|
||||||
| ```bash | ||||||
| curl -X POST "https://us.api.konghq.com/v2/directories/$DIRECTORY_ID/principals" \ | ||||||
| --no-progress-meter --fail-with-body \ | ||||||
| -H "Authorization: Bearer $KONNECT_TOKEN" \ | ||||||
| --json '{ | ||||||
| "display_name": "example-principal", | ||||||
| "description": "Example principal" | ||||||
| }' | ||||||
| ``` | ||||||
|
|
||||||
| Save the returned ID as `$PRINCIPAL_ID`: | ||||||
|
|
||||||
|
|
||||||
| ```bash | ||||||
| export PRINCIPAL_ID='PRINCIPAL_ID' | ||||||
| ``` | ||||||
|
|
||||||
| ## Add basic auth credentials | ||||||
|
|
||||||
| Create basic auth credentials for the Principal: | ||||||
|
|
||||||
| ```bash | ||||||
| curl -X POST "https://us.api.konghq.com/v2/directories/$DIRECTORY_ID/principals/$PRINCIPAL_ID/basic-auths" \ | ||||||
| -H "Authorization: Bearer $KONNECT_TOKEN" \ | ||||||
| --json '{ | ||||||
| "username": "example-user", | ||||||
| "password": "example-password" | ||||||
| }' | ||||||
| ``` | ||||||
|
|
||||||
| This sets the following credentials for the Principal: | ||||||
|
|
||||||
| - Username: `example-user` | ||||||
| - Password: `example-password` | ||||||
|
|
||||||
|
juliamrch marked this conversation as resolved.
Outdated
|
||||||
| ## Configure the Basic Auth plugin via decK | ||||||
|
|
||||||
| Enable the [Basic Authentication plugin](/plugins/basic-auth/) to allow users to authenticate with a username and password when they make a request. | ||||||
|
|
||||||
| 1. Run `echo $DIRECTORY_ID` and copy the output. | ||||||
|
|
||||||
| 2. Paste the value to replace `DIRECTORY_ID`, then run: | ||||||
|
|
||||||
| {% entity_examples %} | ||||||
| entities: | ||||||
| plugins: | ||||||
| - config: | ||||||
| hide_credentials: true | ||||||
| principals: | ||||||
| directory: DIRECTORY_ID | ||||||
| enabled: true | ||||||
| enabled: true | ||||||
| name: basic-auth | ||||||
| protocols: | ||||||
| - http | ||||||
| - https | ||||||
| route: example-route | ||||||
| formats: | ||||||
| - deck | ||||||
| {% endentity_examples %} | ||||||
|
|
||||||
| This configuration: | ||||||
|
|
||||||
| - Enables the plugin globally, which means it applies to all {{site.base_gateway}} Services and Routes. | ||||||
| - Set the authentication method with Principals in the `example-directory` Directory. | ||||||
|
|
||||||
| ## Validate | ||||||
|
|
||||||
| When a Principal authenticates with basic auth, the authorization header must be base64-encoded. For example, since we are using `example-user` as the username and `example-password` as the password, then the field’s value is the base64 encoding of `example-user:example-password`, or `ZXhhbXBsZS11c2VyOmV4YW1wbGUtcGFzc3dvcmQ=`. | ||||||
|
|
||||||
| First, run the following to verify that unauthorized requests return an error: | ||||||
|
|
||||||
| <!--vale off--> | ||||||
| {% validation unauthorized-check %} | ||||||
| url: /anything | ||||||
| headers: | ||||||
| - 'authorization: Basic wrongpassword' | ||||||
| {% endvalidation %} | ||||||
| <!--vale on--> | ||||||
|
|
||||||
| Then, run the following command to test Principal authentication: | ||||||
|
|
||||||
| {% validation request-check %} | ||||||
| url: '/anything' | ||||||
| display_headers: true | ||||||
| headers: | ||||||
| - 'authorization: Basic ZXhhbXBsZS11c2VyOmV4YW1wbGUtcGFzc3dvcmQ=' | ||||||
| status_code: 200 | ||||||
| {% endvalidation %} | ||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.