Skip to content

Commit

Permalink
add auth-service
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <[email protected]>
  • Loading branch information
kobergj committed Jun 13, 2023
1 parent 85dcb57 commit 1691c77
Show file tree
Hide file tree
Showing 24 changed files with 737 additions and 3 deletions.
1 change: 1 addition & 0 deletions .drone.star
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ config = {
"services/auth-basic",
"services/auth-bearer",
"services/auth-machine",
"services/auth-service",
"services/eventhistory",
"services/frontend",
"services/gateway",
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ OCIS_MODULES = \
services/auth-basic \
services/auth-bearer \
services/auth-machine \
services/auth-service \
services/eventhistory \
services/frontend \
services/gateway \
Expand Down
2 changes: 2 additions & 0 deletions ocis-pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
authbasic "github.com/owncloud/ocis/v2/services/auth-basic/pkg/config"
authbearer "github.com/owncloud/ocis/v2/services/auth-bearer/pkg/config"
authmachine "github.com/owncloud/ocis/v2/services/auth-machine/pkg/config"
authservice "github.com/owncloud/ocis/v2/services/auth-service/pkg/config"
eventhistory "github.com/owncloud/ocis/v2/services/eventhistory/pkg/config"
frontend "github.com/owncloud/ocis/v2/services/frontend/pkg/config"
gateway "github.com/owncloud/ocis/v2/services/gateway/pkg/config"
Expand Down Expand Up @@ -82,6 +83,7 @@ type Config struct {
AuthBasic *authbasic.Config `yaml:"auth_basic"`
AuthBearer *authbearer.Config `yaml:"auth_bearer"`
AuthMachine *authmachine.Config `yaml:"auth_machine"`
AuthService *authservice.Config `yaml:"auth_service"`
EventHistory *eventhistory.Config `yaml:"eventhistory"`
Frontend *frontend.Config `yaml:"frontend"`
Gateway *gateway.Config `yaml:"gateway"`
Expand Down
2 changes: 2 additions & 0 deletions ocis-pkg/config/defaultconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
authbasic "github.com/owncloud/ocis/v2/services/auth-basic/pkg/config/defaults"
authbearer "github.com/owncloud/ocis/v2/services/auth-bearer/pkg/config/defaults"
authmachine "github.com/owncloud/ocis/v2/services/auth-machine/pkg/config/defaults"
authservice "github.com/owncloud/ocis/v2/services/auth-service/pkg/config/defaults"
eventhistory "github.com/owncloud/ocis/v2/services/eventhistory/pkg/config/defaults"
frontend "github.com/owncloud/ocis/v2/services/frontend/pkg/config/defaults"
gateway "github.com/owncloud/ocis/v2/services/gateway/pkg/config/defaults"
Expand Down Expand Up @@ -54,6 +55,7 @@ func DefaultConfig() *Config {
AuthBasic: authbasic.DefaultConfig(),
AuthBearer: authbearer.DefaultConfig(),
AuthMachine: authmachine.DefaultConfig(),
AuthService: authservice.DefaultConfig(),
EventHistory: eventhistory.DefaultConfig(),
Frontend: frontend.DefaultConfig(),
Gateway: gateway.DefaultConfig(),
Expand Down
33 changes: 33 additions & 0 deletions ocis/pkg/command/auth-service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package command

import (
"fmt"

"github.com/owncloud/ocis/v2/ocis-pkg/config"
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
"github.com/owncloud/ocis/v2/ocis-pkg/config/parser"
"github.com/owncloud/ocis/v2/ocis/pkg/command/helper"
"github.com/owncloud/ocis/v2/ocis/pkg/register"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/command"
"github.com/urfave/cli/v2"
)

// AuthServiceCommand is the entrypoint for the AuthService command.
func AuthServiceCommand(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: cfg.AuthService.Service.Name,
Usage: helper.SubcommandDescription(cfg.AuthService.Service.Name),
Category: "services",
Before: func(c *cli.Context) error {
configlog.Error(parser.ParseConfig(cfg, true))
cfg.AuthService.Commons = cfg.Commons
fmt.Println("SERVICE", cfg.AuthService.Commons.TokenManager.JWTSecret)
return nil
},
Subcommands: command.GetCommands(cfg.AuthService),
}
}

func init() {
register.AddCommand(AuthServiceCommand)
}
6 changes: 6 additions & 0 deletions ocis/pkg/runtime/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
audit "github.com/owncloud/ocis/v2/services/audit/pkg/command"
authbasic "github.com/owncloud/ocis/v2/services/auth-basic/pkg/command"
authmachine "github.com/owncloud/ocis/v2/services/auth-machine/pkg/command"
authservice "github.com/owncloud/ocis/v2/services/auth-service/pkg/command"
eventhistory "github.com/owncloud/ocis/v2/services/eventhistory/pkg/command"
frontend "github.com/owncloud/ocis/v2/services/frontend/pkg/command"
gateway "github.com/owncloud/ocis/v2/services/gateway/pkg/command"
Expand Down Expand Up @@ -133,6 +134,11 @@ func NewService(options ...Option) (*Service, error) {
cfg.AuthMachine.Commons = cfg.Commons
return authmachine.Execute(cfg.AuthMachine)
})
reg(opts.Config.AuthService.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.AuthService.Context = ctx
cfg.AuthService.Commons = cfg.Commons
return authservice.Execute(cfg.AuthService)
})
reg(opts.Config.EventHistory.Service.Name, func(ctx context.Context, cfg *ociscfg.Config) error {
cfg.EventHistory.Context = ctx
cfg.EventHistory.Commons = cfg.Commons
Expand Down
37 changes: 37 additions & 0 deletions services/auth-service/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
SHELL := bash
NAME := auth-service

include ../../.make/recursion.mk

############ tooling ############
ifneq (, $(shell command -v go 2> /dev/null)) # suppress `command not found warnings` for non go targets in CI
include ../../.bingo/Variables.mk
endif

############ go tooling ############
include ../../.make/go.mk

############ release ############
include ../../.make/release.mk

############ docs generate ############
include ../../.make/docs.mk

.PHONY: docs-generate
docs-generate: config-docs-generate

############ generate ############
include ../../.make/generate.mk

.PHONY: ci-go-generate
ci-go-generate: # CI runs ci-node-generate automatically before this target

.PHONY: ci-node-generate
ci-node-generate:

############ licenses ############
.PHONY: ci-node-check-licenses
ci-node-check-licenses:

.PHONY: ci-node-save-licenses
ci-node-save-licenses:
14 changes: 14 additions & 0 deletions services/auth-service/cmd/auth-service/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"os"

"github.com/owncloud/ocis/v2/services/auth-service/pkg/command"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/config/defaults"
)

func main() {
if err := command.Execute(defaults.DefaultConfig()); err != nil {
os.Exit(1)
}
}
54 changes: 54 additions & 0 deletions services/auth-service/pkg/command/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package command

import (
"fmt"
"net/http"

"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/config"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/logging"
"github.com/urfave/cli/v2"
)

// Health is the entrypoint for the health command.
func Health(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "health",
Usage: "check health status",
Category: "info",
Before: func(c *cli.Context) error {
return configlog.ReturnError(parser.ParseConfig(cfg))
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)

resp, err := http.Get(
fmt.Sprintf(
"http://%s/healthz",
cfg.Debug.Addr,
),
)

if err != nil {
logger.Fatal().
Err(err).
Msg("Failed to request health check")
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
logger.Fatal().
Int("code", resp.StatusCode).
Msg("Health seems to be in bad state")
}

logger.Debug().
Int("code", resp.StatusCode).
Msg("Health got a good state")

return nil
},
}
}
34 changes: 34 additions & 0 deletions services/auth-service/pkg/command/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package command

import (
"os"

"github.com/owncloud/ocis/v2/ocis-pkg/clihelper"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/config"
"github.com/urfave/cli/v2"
)

// GetCommands provides all commands for this service
func GetCommands(cfg *config.Config) cli.Commands {
return []*cli.Command{
// start this service
Server(cfg),

// interaction with this service

// infos about this service
Health(cfg),
Version(cfg),
}
}

// Execute is the entry point for the ocis-auth-service command.
func Execute(cfg *config.Config) error {
app := clihelper.DefaultApp(&cli.App{
Name: "auth-service",
Usage: "Provide service authentication for oCIS",
Commands: GetCommands(cfg),
})

return app.Run(os.Args)
}
99 changes: 99 additions & 0 deletions services/auth-service/pkg/command/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package command

import (
"context"
"fmt"
"os"
"path"

"github.com/cs3org/reva/v2/cmd/revad/runtime"
"github.com/gofrs/uuid"
"github.com/oklog/run"
"github.com/owncloud/ocis/v2/ocis-pkg/config/configlog"
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
"github.com/owncloud/ocis/v2/ocis-pkg/sync"
"github.com/owncloud/ocis/v2/ocis-pkg/version"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/config"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/logging"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/revaconfig"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/server/debug"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/tracing"
"github.com/urfave/cli/v2"
)

// Server is the entry point for the server command.
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "server",
Usage: fmt.Sprintf("start the %s service without runtime (unsupervised mode)", cfg.Service.Name),
Category: "server",
Before: func(c *cli.Context) error {
return configlog.ReturnFatal(parser.ParseConfig(cfg))
},
Action: func(c *cli.Context) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
err := tracing.Configure(cfg, logger)
if err != nil {
return err
}
gr := run.Group{}
ctx, cancel := defineContext(cfg)

defer cancel()

pidFile := path.Join(os.TempDir(), "revad-"+cfg.Service.Name+"-"+uuid.Must(uuid.NewV4()).String()+".pid")

rcfg := revaconfig.AuthMachineConfigFromStruct(cfg)

gr.Add(func() error {
runtime.RunWithOptions(rcfg, pidFile, runtime.WithLogger(&logger.Logger))
return nil
}, func(err error) {
logger.Error().
Err(err).
Str("server", cfg.Service.Name).
Msg("Shutting down server")

cancel()
})

debugServer, err := debug.Server(
debug.Logger(logger),
debug.Context(ctx),
debug.Config(cfg),
)

if err != nil {
logger.Info().Err(err).Str("server", "debug").Msg("Failed to initialize server")
return err
}

gr.Add(debugServer.ListenAndServe, func(_ error) {
cancel()
})

if !cfg.Supervised {
sync.Trap(&gr, cancel)
}

grpcSvc := registry.BuildGRPCService(cfg.GRPC.Namespace+"."+cfg.Service.Name, uuid.Must(uuid.NewV4()).String(), cfg.GRPC.Addr, version.GetString())
if err := registry.RegisterService(ctx, grpcSvc, logger); err != nil {
logger.Fatal().Err(err).Msg("failed to register the grpc service")
}

return gr.Run()
},
}
}

// defineContext sets the context for the service. If there is a context configured it will create a new child from it,
// if not, it will create a root context that can be cancelled.
func defineContext(cfg *config.Config) (context.Context, context.CancelFunc) {
return func() (context.Context, context.CancelFunc) {
if cfg.Context == nil {
return context.WithCancel(context.Background())
}
return context.WithCancel(cfg.Context)
}()
}
50 changes: 50 additions & 0 deletions services/auth-service/pkg/command/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package command

import (
"fmt"
"os"

"github.com/owncloud/ocis/v2/ocis-pkg/registry"
"github.com/owncloud/ocis/v2/ocis-pkg/version"

tw "github.com/olekukonko/tablewriter"
"github.com/owncloud/ocis/v2/services/auth-service/pkg/config"
"github.com/urfave/cli/v2"
)

// Version prints the service versions of all running instances.
func Version(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "version",
Usage: "print the version of this binary and the running service instances",
Category: "info",
Action: func(c *cli.Context) error {
fmt.Println("Version: " + version.GetString())
fmt.Printf("Compiled: %s\n", version.Compiled())
fmt.Println("")

reg := registry.GetRegistry()
services, err := reg.GetService(cfg.GRPC.Namespace + "." + cfg.Service.Name)
if err != nil {
fmt.Println(fmt.Errorf("could not get %s services from the registry: %v", cfg.Service.Name, err))
return err
}

if len(services) == 0 {
fmt.Println("No running " + cfg.Service.Name + " service found.")
return nil
}

table := tw.NewWriter(os.Stdout)
table.SetHeader([]string{"Version", "Address", "Id"})
table.SetAutoFormatHeaders(false)
for _, s := range services {
for _, n := range s.Nodes {
table.Append([]string{s.Version, n.Address, n.Id})
}
}
table.Render()
return nil
},
}
}
Loading

0 comments on commit 1691c77

Please sign in to comment.