Add authentication to worker
This commit is contained in:
parent
ad75b5c705
commit
082e2e69d6
@ -2,10 +2,12 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/deepmap/oapi-codegen/pkg/securityprovider"
|
||||||
"github.com/mattn/go-colorable"
|
"github.com/mattn/go-colorable"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@ -20,17 +22,30 @@ func main() {
|
|||||||
|
|
||||||
log.Info().Str("version", appinfo.ApplicationVersion).Msgf("starting %v Worker", appinfo.ApplicationName)
|
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 {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("error creating client")
|
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()
|
hostname, err := os.Hostname()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("error getting hostname")
|
log.Fatal().Err(err).Msg("error getting hostname")
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
|
||||||
req := api.RegisterWorkerJSONRequestBody{
|
req := api.RegisterWorkerJSONRequestBody{
|
||||||
Nickname: hostname,
|
Nickname: hostname,
|
||||||
Platform: runtime.GOOS,
|
Platform: runtime.GOOS,
|
||||||
@ -44,13 +59,38 @@ func main() {
|
|||||||
switch {
|
switch {
|
||||||
case resp.JSON200 != nil:
|
case resp.JSON200 != nil:
|
||||||
log.Info().
|
log.Info().
|
||||||
Int("code", resp.HTTPResponse.StatusCode).
|
Int("code", resp.StatusCode()).
|
||||||
Interface("resp", resp.JSON200).
|
Interface("resp", resp.JSON200).
|
||||||
Msg("registered at Manager")
|
Msg("registered at Manager")
|
||||||
default:
|
default:
|
||||||
log.Warn().
|
log.Fatal().
|
||||||
Int("code", resp.HTTPResponse.StatusCode).
|
Int("code", resp.StatusCode()).
|
||||||
Interface("resp", resp.JSONDefault).
|
Interface("resp", resp.JSONDefault).
|
||||||
Msg("unable to register at Manager")
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -52,7 +52,11 @@ paths:
|
|||||||
content:
|
content:
|
||||||
application/json:
|
application/json:
|
||||||
schema: {$ref: "#/components/schemas/AssignedTask"}
|
schema: {$ref: "#/components/schemas/AssignedTask"}
|
||||||
|
"403":
|
||||||
|
description: Permission Denied
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema: {$ref: "#/components/schemas/SecurityError"}
|
||||||
|
|
||||||
components:
|
components:
|
||||||
schemas:
|
schemas:
|
||||||
@ -120,11 +124,14 @@ components:
|
|||||||
type: object
|
type: object
|
||||||
required: [code, message]
|
required: [code, message]
|
||||||
properties:
|
properties:
|
||||||
code:
|
code: {type: integer, format: int32}
|
||||||
type: integer
|
message: {type: string}
|
||||||
format: int32
|
|
||||||
message:
|
SecurityError:
|
||||||
type: string
|
type: object
|
||||||
|
required: [message]
|
||||||
|
properties:
|
||||||
|
message: {type: string}
|
||||||
|
|
||||||
securitySchemes:
|
securitySchemes:
|
||||||
worker_auth:
|
worker_auth:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user