This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Feature/files api #540
Draft
dbampalikis
wants to merge
27
commits into
master
Choose a base branch
from
feature/files-api
base: master
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.
Draft
Feature/files api #540
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
a1e5f58
Add configuration for jwt key
dbampalikis 8461351
Add database call for getting files per user
dbampalikis c8f3266
Change API framework to gin
dbampalikis 723a63f
Add get files API call
dbampalikis fb7348c
Add tests for api
dbampalikis 26b08ac
Update to gin framework in integration test
dbampalikis 24820c4
Update database query in get user files
dbampalikis 6c820fb
Change JWT library in get user files
dbampalikis d10ec02
Update tests in get user files
dbampalikis 6894ccc
Fix spelling mistakes
dbampalikis a3470e5
Update config files for API
dbampalikis 0b7f61b
Update config
dbampalikis 2ed1f83
Update api in docker compose files
dbampalikis de05fe3
Fix linter issues
dbampalikis 9d0cacb
Add script to create jwt in tests
dbampalikis 858bd28
Add api to integration testing
dbampalikis bac5c7c
Add check for api response in integration testing
dbampalikis 691b031
Remove unused variable from api tests
dbampalikis 33c3b46
Storage_test don't look for static files
jbygdell e9bffbc
[Api integration test] start containers from within the test
jbygdell 8962071
Update go mod and sum
dbampalikis 1112f6c
Remove unused variable from `getUserFromToken`
jbygdell b3d17f4
Remove unused variable from `CreateRSAToken`
jbygdell e46bce5
Make script run at a proper time
jbygdell 5136c88
Put public keys in a separate folder
jbygdell a48bdec
Don't expose token in logs
jbygdell 58e99fb
Fail test if API returns bad value
jbygdell 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
This file contains 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,71 @@ | ||
#!/usr/bin/bash | ||
|
||
# Create RSA keys | ||
cd dev_utils || exit 1 | ||
mkdir -p keys/pub || exit 1 | ||
cd keys || exit 1 | ||
|
||
ssh-keygen -t rsa -b 4096 -m PEM -f example.demo.pem -q -N "" | ||
openssl rsa -in example.demo.pem -pubout -outform PEM -out pub/example.demo.pub | ||
|
||
# Shared content to use as template | ||
header_template='{ | ||
"typ": "JWT", | ||
"kid": "0001" | ||
}' | ||
|
||
build_header() { | ||
jq -c \ | ||
--arg iat_str "$(date +%s)" \ | ||
--arg alg "${1}" \ | ||
' | ||
($iat_str | tonumber) as $iat | ||
| .alg = $alg | ||
| .iat = $iat | ||
| .exp = ($iat + 86400) | ||
' <<<"$header_template" | tr -d '\n' | ||
} | ||
|
||
b64enc() { openssl enc -base64 -A | tr '+/' '-_' | tr -d '='; } | ||
json() { jq -c . | LC_CTYPE=C tr -d '\n'; } | ||
rs_sign() { openssl dgst -binary -sha"${1}" -sign <(printf '%s\n' "$2"); } | ||
es_sign() { openssl dgst -binary -sha"${1}" -sign <(printf '%s\n' "$2") | openssl asn1parse -inform DER | grep INTEGER | cut -d ':' -f 4 | xxd -p -r; } | ||
|
||
sign() { | ||
if [ -n "$2" ]; then | ||
rsa_secret=$(<"$2") | ||
else | ||
echo "no signing key supplied" | ||
exit 1 | ||
fi | ||
local algo payload header sig secret=$rsa_secret | ||
algo=${1:-RS256} | ||
algo=${algo^^} | ||
header=$(build_header "$algo") || return | ||
payload=${4:-$test_payload} | ||
signed_content="$(json <<<"$header" | b64enc).$(json <<<"$payload" | b64enc)" | ||
case $algo in | ||
RS*) sig=$(printf %s "$signed_content" | rs_sign "${algo#RS}" "$secret" | b64enc) ;; | ||
ES*) sig=$(printf %s "$signed_content" | es_sign "${algo#ES}" "$secret" | b64enc) ;; | ||
*) | ||
echo "Unknown algorithm" >&2 | ||
return 1 | ||
;; | ||
esac | ||
printf '%s.%s\n' "${signed_content}" "${sig}" | ||
} | ||
|
||
iat=$(date +%s) | ||
exp=$(date --date="${3:-tomorrow}" +%s) | ||
|
||
test_payload='{ | ||
"sub": "test", | ||
"azp": "azp", | ||
"scope": "openid ga4gh_passport_v1", | ||
"iss": "http://example.demo", | ||
"exp": '"$exp"', | ||
"iat": '"$iat"', | ||
"jti": "6ad7aa42-3e9c-4833-bd16-765cb80c2102" | ||
}' | ||
|
||
sign RS256 example.demo.pem > token.jwt |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -3,18 +3,24 @@ package main | |
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
"sda-pipeline/internal/broker" | ||
"sda-pipeline/internal/config" | ||
"sda-pipeline/internal/database" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/gin-gonic/gin" | ||
"github.com/lestrrat-go/jwx/v2/jwa" | ||
"github.com/lestrrat-go/jwx/v2/jwt" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
@@ -36,6 +42,13 @@ func main() { | |
log.Fatal(err) | ||
} | ||
|
||
Conf.API.JtwKeys = make(map[string][]byte) | ||
if Conf.API.JwtPubKeyPath != "" { | ||
if err := config.GetJwtKey(Conf.API.JwtPubKeyPath, Conf.API.JtwKeys); err != nil { | ||
log.Panicf("Error while getting key %s: %v", Conf.API.JwtPubKeyPath, err) | ||
} | ||
} | ||
|
||
sigc := make(chan os.Signal, 5) | ||
signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) | ||
go func() { | ||
|
@@ -62,9 +75,11 @@ func main() { | |
} | ||
|
||
func setup(config *config.Config) *http.Server { | ||
r := mux.NewRouter().SkipClean(true) | ||
|
||
r.HandleFunc("/ready", readinessResponse).Methods("GET") | ||
r := gin.Default() | ||
|
||
r.GET("/ready", readinessResponse) | ||
r.GET("/files", getFiles) | ||
|
||
cfg := &tls.Config{ | ||
MinVersion: tls.VersionTLS12, | ||
|
@@ -94,11 +109,11 @@ func shutdown() { | |
defer Conf.API.DB.Close() | ||
} | ||
|
||
func readinessResponse(w http.ResponseWriter, r *http.Request) { | ||
statusCocde := http.StatusOK | ||
func readinessResponse(c *gin.Context) { | ||
statusCode := http.StatusOK | ||
|
||
if Conf.API.MQ.Connection.IsClosed() { | ||
statusCocde = http.StatusServiceUnavailable | ||
statusCode = http.StatusServiceUnavailable | ||
newConn, err := broker.NewMQ(Conf.Broker) | ||
if err != nil { | ||
log.Errorf("failed to reconnect to MQ, reason: %v", err) | ||
|
@@ -108,7 +123,7 @@ func readinessResponse(w http.ResponseWriter, r *http.Request) { | |
} | ||
|
||
if Conf.API.MQ.Channel.IsClosed() { | ||
statusCocde = http.StatusServiceUnavailable | ||
statusCode = http.StatusServiceUnavailable | ||
Conf.API.MQ.Connection.Close() | ||
newConn, err := broker.NewMQ(Conf.Broker) | ||
if err != nil { | ||
|
@@ -121,10 +136,10 @@ func readinessResponse(w http.ResponseWriter, r *http.Request) { | |
if DBRes := checkDB(Conf.API.DB, 5*time.Millisecond); DBRes != nil { | ||
log.Debugf("DB connection error :%v", DBRes) | ||
Conf.API.DB.Reconnect() | ||
statusCocde = http.StatusServiceUnavailable | ||
statusCode = http.StatusServiceUnavailable | ||
} | ||
|
||
w.WriteHeader(statusCocde) | ||
c.JSON(statusCode, "") | ||
} | ||
|
||
func checkDB(database *database.SQLdb, timeout time.Duration) error { | ||
|
@@ -136,3 +151,114 @@ func checkDB(database *database.SQLdb, timeout time.Duration) error { | |
|
||
return database.DB.PingContext(ctx) | ||
} | ||
|
||
// getFiles returns the files from the database for a specific user | ||
func getFiles(c *gin.Context) { | ||
|
||
log.Debugf("request files in project") | ||
c.Writer.Header().Set("Content-Type", "application/json") | ||
// Get user ID to extract all files | ||
userID, err := getUserFromToken(c.Request) | ||
if err != nil { | ||
// something went wrong with user token | ||
c.JSON(500, err.Error()) | ||
|
||
return | ||
} | ||
|
||
files, err := Conf.API.DB.GetUserFiles(userID) | ||
if err != nil { | ||
// something went wrong with querying or parsing rows | ||
c.JSON(500, err.Error()) | ||
|
||
return | ||
} | ||
|
||
// Return response | ||
c.JSON(200, files) | ||
} | ||
|
||
// getUserFromToken parses the token, validates it against the key and returns the 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. i think the token check should be split, so that it is easier to integrate with sda download: see: https://github.com/neicnordic/sda-download/blob/main/pkg/auth/auth.go#L54-L90 Also it would be good to have a list of trusted issuers: https://github.com/neicnordic/sda-download/blob/main/pkg/auth/auth.go#L327-L344 |
||
func getUserFromToken(r *http.Request) (string, error) { | ||
// Check that a token is provided | ||
tokenStr, err := getToken(r.Header.Get("Authorization")) | ||
if err != nil { | ||
log.Error("authorization header missing from request") | ||
|
||
return "", fmt.Errorf("could not get token from header: %v", err) | ||
} | ||
|
||
token, err := jwt.Parse([]byte(tokenStr), jwt.WithVerify(false)) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to get parse token: %v", err) | ||
} | ||
strIss := token.Issuer() | ||
|
||
// Poor string unescaper for elixir | ||
strIss = strings.ReplaceAll(strIss, "\\", "") | ||
|
||
log.Debugf("Looking for key for %s", strIss) | ||
|
||
iss, err := url.ParseRequestURI(strIss) | ||
if err != nil || iss.Hostname() == "" { | ||
return "", fmt.Errorf("failed to get issuer from token (%v)", strIss) | ||
} | ||
|
||
block, _ := pem.Decode(Conf.API.JtwKeys[iss.Hostname()]) | ||
key, err := x509.ParsePKIXPublicKey(block.Bytes) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to parse key (%v)", err) | ||
} | ||
|
||
verifiedToken, err := jwt.Parse([]byte(tokenStr), jwt.WithKey(jwa.RS256, key)) | ||
if err != nil { | ||
log.Debugf("failed to verify token as RS256 signature of token %s", err) | ||
verifiedToken, err = jwt.Parse([]byte(tokenStr), jwt.WithKey(jwa.ES256, key)) | ||
if err != nil { | ||
log.Errorf("failed to verify token as ES256 signature of token %s", err) | ||
|
||
return "", fmt.Errorf("failed to verify token as RSA256 or ES256 signature of token %s", err) | ||
} | ||
} | ||
|
||
return verifiedToken.Subject(), nil | ||
} | ||
|
||
// getToken parses the token string from header | ||
func getToken(header string) (string, error) { | ||
log.Debug("parsing access token from header") | ||
|
||
if len(header) == 0 { | ||
log.Error("authorization check failed, empty header") | ||
|
||
return "", fmt.Errorf("access token must be provided") | ||
} | ||
|
||
// Check that Bearer scheme is used | ||
headerParts := strings.Split(header, " ") | ||
if headerParts[0] != "Bearer" { | ||
log.Error("authorization check failed, no Bearer on header") | ||
|
||
return "", fmt.Errorf("authorization scheme must be bearer") | ||
} | ||
|
||
// Check that header contains a token string | ||
var token string | ||
if len(headerParts) == 2 { | ||
token = headerParts[1] | ||
} else { | ||
log.Error("authorization check failed, no token on header") | ||
|
||
return "", fmt.Errorf("token string is missing from authorization header") | ||
} | ||
|
||
if len(token) < 2 { | ||
log.Error("authorization check failed, too small token") | ||
|
||
return "", fmt.Errorf("token string is missing from authorization header") | ||
} | ||
|
||
log.Debug("access token found") | ||
|
||
return token, nil | ||
} |
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.
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.
not sure i follow why we need MQ checks here,the API does not do anything with the broker.