Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Feature/files api #540

Draft
wants to merge 27 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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 Feb 27, 2023
8461351
Add database call for getting files per user
dbampalikis Feb 27, 2023
c8f3266
Change API framework to gin
dbampalikis Feb 27, 2023
723a63f
Add get files API call
dbampalikis Feb 27, 2023
fb7348c
Add tests for api
dbampalikis Feb 27, 2023
26b08ac
Update to gin framework in integration test
dbampalikis Feb 27, 2023
24820c4
Update database query in get user files
dbampalikis Mar 2, 2023
6c820fb
Change JWT library in get user files
dbampalikis Mar 2, 2023
d10ec02
Update tests in get user files
dbampalikis Mar 2, 2023
6894ccc
Fix spelling mistakes
dbampalikis Mar 2, 2023
a3470e5
Update config files for API
dbampalikis Mar 2, 2023
0b7f61b
Update config
dbampalikis Mar 2, 2023
2ed1f83
Update api in docker compose files
dbampalikis Mar 2, 2023
de05fe3
Fix linter issues
dbampalikis Mar 2, 2023
9d0cacb
Add script to create jwt in tests
dbampalikis Mar 3, 2023
858bd28
Add api to integration testing
dbampalikis Mar 3, 2023
bac5c7c
Add check for api response in integration testing
dbampalikis Mar 3, 2023
691b031
Remove unused variable from api tests
dbampalikis Mar 3, 2023
33c3b46
Storage_test don't look for static files
jbygdell Apr 17, 2023
e9bffbc
[Api integration test] start containers from within the test
jbygdell Apr 17, 2023
8962071
Update go mod and sum
dbampalikis Mar 6, 2023
1112f6c
Remove unused variable from `getUserFromToken`
jbygdell Apr 17, 2023
b3d17f4
Remove unused variable from `CreateRSAToken`
jbygdell Apr 17, 2023
e46bce5
Make script run at a proper time
jbygdell Apr 17, 2023
5136c88
Put public keys in a separate folder
jbygdell Apr 17, 2023
a48bdec
Don't expose token in logs
jbygdell Apr 17, 2023
58e99fb
Fail test if API returns bad value
jbygdell Apr 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions .github/integration/setup/common/09_create_jwt.sh
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
2 changes: 1 addition & 1 deletion .github/integration/setup/common/10_services.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ else

docker-compose -f compose-sda.yml up -d

for p in ingest verify finalize mapper intercept backup; do
for p in ingest verify finalize mapper intercept backup api; do
RETRY_TIMES=0
until docker ps -f name="$p" --format "{{.Status}}" | grep "Up"
do echo "waiting for $p to become ready"
Expand Down
8 changes: 8 additions & 0 deletions .github/integration/tests/common/30_ingest_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,11 @@ docker run --rm --name client --network dev_utils_default -v "$PWD/certs:/certs"
-e PGSSLCERT=/certs/client.pem -e PGSSLKEY=/certs/client-key.pem -e PGSSLROOTCERT=/certs/ca.pem \
neicnordic/pg-client:latest postgresql://postgres:rootpassword@db:5432/lega \
-t -c "SELECT id, status, stable_id, archive_path FROM local_ega.files ORDER BY id DESC"

# Test the API files endpoint
token="$(cat keys/token.jwt)"
response="$(curl -k -L "https://localhost:8080/files" -H "Authorization: Bearer $token" | jq -r 'sort_by(.inboxPath)|.[-1].fileStatus')"
if [ "$response" != "ready" ]; then
echo "API returned incorrect value, expected ready got: $response"
exit 1
fi
144 changes: 135 additions & 9 deletions cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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() {
Expand All @@ -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,
Expand Down Expand Up @@ -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
Copy link
Contributor

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.

newConn, err := broker.NewMQ(Conf.Broker)
if err != nil {
log.Errorf("failed to reconnect to MQ, reason: %v", err)
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
Loading