Skip to content

Commit

Permalink
split auth and node management into two functions
Browse files Browse the repository at this point in the history
  • Loading branch information
djelusic committed Feb 28, 2022
1 parent 772003e commit 9e72dbb
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 118 deletions.
4 changes: 3 additions & 1 deletion cli/controller/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/url"

"github.com/mantil-io/mantil.go/logs"
Expand All @@ -19,7 +20,7 @@ const (
clientID = "db4946aabe86cd6c126e"
)

func authToken(n *domain.Node) (string, error) {
func AuthToken(n *domain.Node) (string, error) {
t, err := n.AuthToken()
var terr *domain.TokenExpiredError
if errors.As(err, &terr) && n.GitHubAuthEnabled {
Expand Down Expand Up @@ -96,6 +97,7 @@ func waitToken(inbox string) (string, error) {
return "", err
}
if err := l.Done(context.Background()); err != nil {
fmt.Println(err)
return "", err
}
return rsp.JWT, nil
Expand Down
4 changes: 2 additions & 2 deletions cli/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func awsClient(node *domain.Node, stage *domain.Stage) (*aws.AWS, error) {
url.RawQuery = q.Encode()

token := func() string {
token, err := authToken(node)
token, err := AuthToken(node)
if err != nil {
return ""
}
Expand All @@ -93,7 +93,7 @@ func awsClient(node *domain.Node, stage *domain.Stage) (*aws.AWS, error) {
}

func nodeInvoker(node *domain.Node) (*invoke.HTTPClient, error) {
token, err := authToken(node)
token, err := AuthToken(node)
if err != nil {
return nil, log.Wrap(err)
}
Expand Down
11 changes: 8 additions & 3 deletions cli/controller/examples/node_example.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package examples

import (
"github.com/mantil-io/mantil/cli/controller"
"github.com/mantil-io/mantil/cli/controller/invoke"
"github.com/mantil-io/mantil/cli/ui"
"github.com/mantil-io/mantil/domain"
Expand Down Expand Up @@ -35,7 +36,7 @@ func NewUserAddCommand() *cobra.Command {
if err != nil {
return err
}
return i.Do("auth/addUser", &AddUserRequest{
return i.Do("node/addUser", &AddUserRequest{
Username: args[0],
}, nil)
},
Expand Down Expand Up @@ -72,7 +73,7 @@ func NewProjectAddCommand() *cobra.Command {
if err != nil {
return err
}
return i.Do("auth/addProject", &AddProjectRequest{
return i.Do("node/addProject", &AddProjectRequest{
Repo: args[0],
}, nil)
},
Expand All @@ -82,5 +83,9 @@ func NewProjectAddCommand() *cobra.Command {
}

func nodeInvoker(node *domain.Node) (*invoke.HTTPClient, error) {
return invoke.Node(node.Endpoints.Rest, "", ui.NodeLogsSink), nil
t, err := controller.AuthToken(node)
if err != nil {
return nil, err
}
return invoke.Node(node.Endpoints.Rest, t, ui.NodeLogsSink), nil
}
45 changes: 0 additions & 45 deletions node/api/auth/auth.go

This file was deleted.

59 changes: 0 additions & 59 deletions node/api/auth/store.go

This file was deleted.

32 changes: 26 additions & 6 deletions node/api/auth/jwt.go → node/api/node/auth.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
package auth
package node

import (
"context"
"encoding/json"
"fmt"
"log"
"time"

"github.com/google/go-github/v42/github"
"github.com/mantil-io/mantil.go/logs"
"github.com/mantil-io/mantil/cli/log"
"github.com/mantil-io/mantil/cli/secret"
"github.com/mantil-io/mantil/domain"
"github.com/mantil-io/mantil/kit/aws"
"github.com/mantil-io/mantil/kit/token"
"golang.org/x/oauth2"
)

type Auth struct {
JWTRequest *JWTRequest
store *Store
ghClient *github.Client
natsPublisher *logs.Publisher
privateKey string
githubUsername string
githubOrg string
}

func NewAuth() *Auth {
s, err := NewStore()
if err != nil {
log.Fatal(err)
}
return &Auth{
store: s,
}
}

type JWTRequest struct {
Inbox string `json:"inbox"`
GithubToken string `json:"github_token"`
Expand Down Expand Up @@ -91,11 +111,11 @@ func (a *Auth) generateJWT() (string, error) {
return a.ownerToken(*ghUser.Login)
case domain.Member:
// check if user is allowed to access the node
user, err := a.findUser(*ghUser.Login)
user, err := a.store.FindUser(*ghUser.Login)
if err != nil {
return "", err
}
projects, err := a.findProjects()
projects, err := a.store.FindProjects()
if err != nil {
return "", err
}
Expand Down Expand Up @@ -162,9 +182,9 @@ func (a *Auth) publishJWT(jwt string) error {

func (a *Auth) publishError(e error) {
if err := a.natsPublisher.Error(e); err != nil {
log.Error(err)
log.Println(err)
}
if err := a.natsPublisher.Close(); err != nil {
log.Error(err)
log.Println(err)
}
}
65 changes: 65 additions & 0 deletions node/api/node/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package node

import "github.com/mantil-io/mantil.go"

const (
usersPartition = "users"
projectsPartition = "projects"
)

type Store struct {
users *mantil.KV
projects *mantil.KV
}

func NewStore() (*Store, error) {
users, err := mantil.NewKV(usersPartition)
if err != nil {
return nil, err
}
projects, err := mantil.NewKV(projectsPartition)
if err != nil {
return nil, err
}
return &Store{
users: users,
projects: projects,
}, nil
}

type user struct {
Name string
}

func (s *Store) StoreUser(name string) error {
return s.users.Put(name, &user{
Name: name,
})
}

func (s *Store) FindUser(name string) (*user, error) {
u := &user{}
if err := s.users.Get(name, u); err != nil {
return nil, err
}
return u, nil
}

type project struct {
Repo string
}

func (s *Store) StoreProject(repo string) error {
return s.projects.Put(repo, &project{
Repo: repo,
})
}

func (s *Store) FindProjects() ([]*project, error) {
projects := []*project{}
_, err := s.projects.FindAll(&projects)
if err != nil {
return nil, err
}
return projects, nil
}
4 changes: 2 additions & 2 deletions node/functions/auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import (
"github.com/mantil-io/mantil.go"
"github.com/mantil-io/mantil/node/api/auth"
"github.com/mantil-io/mantil/node/api/node"
)

func main() {
var api = auth.New()
var api = node.NewAuth()
mantil.LambdaHandler(api)
}
1 change: 1 addition & 0 deletions node/functions/node/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bootstrap
44 changes: 44 additions & 0 deletions node/functions/node/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"context"
"log"

"github.com/mantil-io/mantil.go"
"github.com/mantil-io/mantil/node/api/node"
)

type Node struct {
store *node.Store
}

func New() *Node {
s, err := node.NewStore()
if err != nil {
log.Fatal(err)
}
return &Node{
store: s,
}
}

type AddUserRequest struct {
Username string `json:"username"`
}

func (n *Node) AddUser(ctx context.Context, req *AddUserRequest) error {
return n.store.StoreUser(req.Username)
}

type AddProjectRequest struct {
Repo string `json:"repo"`
}

func (n *Node) AddProject(ctx context.Context, req *AddProjectRequest) error {
return n.store.StoreProject(req.Repo)
}

func main() {
var api = New()
mantil.LambdaHandler(api)
}
Loading

0 comments on commit 9e72dbb

Please sign in to comment.