-
Notifications
You must be signed in to change notification settings - Fork 186
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
enhancement: Add token based auth as alternative to basic-auth for http requests. #192
base: master
Are you sure you want to change the base?
Changes from 1 commit
1f7655d
6a278b8
77ba05f
29e3411
702670e
d1da522
347808a
8335ced
953483a
77de590
41e907c
27488a9
8cb9754
f66fee4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,6 +105,51 @@ spec: | |
defaultMode: 0777 | ||
--- | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: token-auth-sample-secret | ||
type: Opaque | ||
data: | ||
REQ_TOKEN_KEY: private_token | ||
REQ_TOKEN_VALUE: c3VwZXItZHVwZXItc2VjcmV0 | ||
--- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: sidecar-api_key | ||
namespace: default | ||
spec: | ||
serviceAccountName: sample-acc | ||
containers: | ||
- name: sidecar | ||
image: kiwigrid/k8s-sidecar:testing | ||
volumeMounts: | ||
- name: shared-volume | ||
mountPath: /tmp/ | ||
- name: script-volume | ||
mountPath: /opt/script.sh | ||
subPath: script.sh | ||
envFrom: | ||
- secretRef: | ||
name: token-auth-sample-secret | ||
env: | ||
- name: LABEL | ||
value: "findme" | ||
- name: FOLDER | ||
value: /tmp/ | ||
- name: RESOURCE | ||
value: both | ||
- name: SCRIPT | ||
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. and here 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. good point. removed the script 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. also switched resource to configmap |
||
value: "/opt/script.sh" | ||
volumes: | ||
- name: shared-volume | ||
emptyDir: {} | ||
- name: script-volume | ||
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. and here 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. good point. removed the script |
||
configMap: | ||
name: script-configmap | ||
defaultMode: 0777 | ||
--- | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: dummy-server-pod | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
from fastapi import FastAPI | ||
import uvicorn | ||
#from http.client import HTTPException, HTTPResponse | ||
from fastapi import FastAPI, Security, Depends, HTTPException | ||
from fastapi.security.api_key import APIKeyQuery, APIKey | ||
#import uvicorn | ||
jekkel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
API_KEY_NAME="private_token" | ||
API_KEY="super-duper-secret" | ||
api_key_query = APIKeyQuery(name=API_KEY_NAME, auto_error=True) | ||
|
||
app = FastAPI() | ||
|
||
def get_api_key (api_key_query: str = Security(api_key_query)): | ||
if api_key_query == API_KEY: | ||
return api_key_query | ||
else: | ||
raise HTTPException(403) | ||
|
||
@app.get("/", status_code=200) | ||
def read_root(): | ||
|
@@ -27,3 +38,9 @@ async def read_item(): | |
@app.post("/503", status_code=503) | ||
async def read_item(): | ||
return 503 | ||
|
||
|
||
@app.get("/200/api_key") | ||
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. could you add another endpoint with a different API key so we can add a negative test case as well? That would be awesome! 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. added a new container with wrong credentials/secret and used the same endpoint |
||
def read_root(api_key: APIKey = Depends(get_api_key)): | ||
return 200 | ||
|
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.
I think we don't need the script here.
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.
good point. removed the script