Add authentication to worker

This commit is contained in:
Sybren A. Stüvel 2022-01-07 14:36:59 +01:00 committed by Sybren A. Stüvel
parent ad75b5c705
commit 082e2e69d6
2 changed files with 58 additions and 11 deletions

View File

@ -2,10 +2,12 @@ package main
import (
"context"
"net/http"
"os"
"runtime"
"time"
"github.com/deepmap/oapi-codegen/pkg/securityprovider"
"github.com/mattn/go-colorable"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
@ -20,17 +22,30 @@ func main() {
log.Info().Str("version", appinfo.ApplicationVersion).Msgf("starting %v Worker", appinfo.ApplicationName)
flamenco, err := api.NewClientWithResponses("http://localhost:8080/")
basicAuthProvider, err := securityprovider.NewSecurityProviderBasicAuth("MY_USER", "MY_PASS")
if err != nil {
log.Panic().Err(err).Msg("unable to create basic authr")
}
flamenco, err := api.NewClientWithResponses(
"http://localhost:8080/",
api.WithRequestEditorFn(basicAuthProvider.Intercept),
)
if err != nil {
log.Fatal().Err(err).Msg("error creating client")
}
ctx := context.Background()
registerWorker(ctx, flamenco)
obtainTask(ctx, flamenco)
}
func registerWorker(ctx context.Context, flamenco *api.ClientWithResponses) {
hostname, err := os.Hostname()
if err != nil {
log.Fatal().Err(err).Msg("error getting hostname")
}
ctx := context.Background()
req := api.RegisterWorkerJSONRequestBody{
Nickname: hostname,
Platform: runtime.GOOS,
@ -44,13 +59,38 @@ func main() {
switch {
case resp.JSON200 != nil:
log.Info().
Int("code", resp.HTTPResponse.StatusCode).
Int("code", resp.StatusCode()).
Interface("resp", resp.JSON200).
Msg("registered at Manager")
default:
log.Warn().
Int("code", resp.HTTPResponse.StatusCode).
log.Fatal().
Int("code", resp.StatusCode()).
Interface("resp", resp.JSONDefault).
Msg("unable to register at Manager")
}
}
func obtainTask(ctx context.Context, flamenco *api.ClientWithResponses) {
resp, err := flamenco.ScheduleTaskWithResponse(ctx)
if err != nil {
log.Fatal().Err(err).Msg("error obtaining task")
}
switch {
case resp.JSON200 != nil:
log.Info().
Interface("task", resp.JSON200).
Msg("obtained task")
case resp.JSON403 != nil:
log.Fatal().
Int("code", resp.StatusCode()).
Str("error", string(resp.JSON403.Message)).
Msg("access denied")
case resp.StatusCode() == http.StatusNoContent:
log.Info().Msg("no task available")
default:
log.Fatal().
Int("code", resp.StatusCode()).
Str("error", string(resp.Body)).
Msg("unable to obtain task")
}
}

View File

@ -52,7 +52,11 @@ paths:
content:
application/json:
schema: {$ref: "#/components/schemas/AssignedTask"}
"403":
description: Permission Denied
content:
application/json:
schema: {$ref: "#/components/schemas/SecurityError"}
components:
schemas:
@ -120,11 +124,14 @@ components:
type: object
required: [code, message]
properties:
code:
type: integer
format: int32
message:
type: string
code: {type: integer, format: int32}
message: {type: string}
SecurityError:
type: object
required: [message]
properties:
message: {type: string}
securitySchemes:
worker_auth: