Skip to content

Commit

Permalink
refactor: separate generateAgent func
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
adityathebe committed Aug 4, 2023
1 parent fc3faaf commit 9960e54
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
24 changes: 20 additions & 4 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package agent
import (
"fmt"

"github.com/flanksource/commons/rand"
"github.com/flanksource/incident-commander/api"
"github.com/flanksource/incident-commander/db"
"github.com/flanksource/incident-commander/rbac"
"golang.org/x/crypto/bcrypt"
)

// generateAgent creates a new person and a new agent and associates them.
func generateAgent(ctx *api.Context, body api.GenerateAgentRequest) (*api.GeneratedAgent, error) {
username, password, err := genUsernamePassword()
if err != nil {
Expand All @@ -20,22 +22,36 @@ func generateAgent(ctx *api.Context, body api.GenerateAgentRequest) (*api.Genera
return nil, fmt.Errorf("failed to hash password: %w", err)
}

id, err := db.CreatePerson(ctx, username, string(hashedPassword))
person, err := db.CreatePerson(ctx, username, string(hashedPassword))
if err != nil {
return nil, fmt.Errorf("failed to create a new person: %w", err)
}

if _, err := rbac.Enforcer.AddRoleForUser(id.String(), "agent"); err != nil {
if _, err := rbac.Enforcer.AddRoleForUser(person.ID.String(), "agent"); err != nil {
return nil, fmt.Errorf("failed to add 'agent' role to the new person: %w", err)
}

if err := db.CreateAgent(ctx, body.Name, &id, body.Properties); err != nil {
if err := db.CreateAgent(ctx, body.Name, &person.ID, body.Properties); err != nil {
return nil, fmt.Errorf("failed to create a new agent: %w", err)
}

return &api.GeneratedAgent{
ID: id.String(),
ID: person.ID.String(),
Username: username,
AccessToken: password,
}, nil
}

func genUsernamePassword() (username, password string, err error) {
username, err = rand.GenerateRandHex(8)
if err != nil {
return "", "", err
}

password, err = rand.GenerateRandHex(32)
if err != nil {
return "", "", err
}

return fmt.Sprintf("agent-%s", username), password, nil
}
16 changes: 0 additions & 16 deletions agent/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package agent

import (
"encoding/json"
"fmt"
"net/http"

"github.com/flanksource/commons/logger"
crand "github.com/flanksource/commons/rand"
"github.com/flanksource/incident-commander/api"
"github.com/labstack/echo/v4"
)
Expand All @@ -28,17 +26,3 @@ func GenerateAgent(c echo.Context) error {

return c.JSON(http.StatusCreated, agent)
}

func genUsernamePassword() (username, password string, err error) {
username, err = crand.GenerateRandHex(8)
if err != nil {
return "", "", err
}

password, err = crand.GenerateRandHex(32)
if err != nil {
return "", "", err
}

return fmt.Sprintf("agent-%s", username), password, nil
}
11 changes: 5 additions & 6 deletions db/people.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/flanksource/duty/models"
"github.com/flanksource/incident-commander/api"
"github.com/flanksource/incident-commander/utils"
"github.com/google/uuid"
"gorm.io/gorm/clause"
)

Expand All @@ -28,13 +27,13 @@ func UpdateIdentityState(ctx *api.Context, id, state string) error {
return ctx.DB().Table("identities").Where("id = ?", id).Update("state", state).Error
}

func CreatePerson(ctx *api.Context, username, hashedPassword string) (uuid.UUID, error) {
func CreatePerson(ctx *api.Context, username, hashedPassword string) (*models.Person, error) {
tx := ctx.DB().Begin()
defer tx.Rollback()

person := models.Person{Name: username, Type: "agent"}
if err := tx.Clauses(clause.Returning{Columns: []clause.Column{{Name: "id"}}}).Create(&person).Error; err != nil {
return uuid.Nil, err
if err := tx.Clauses(clause.Returning{}).Create(&person).Error; err != nil {
return nil, err
}

accessToken := models.AccessToken{
Expand All @@ -43,8 +42,8 @@ func CreatePerson(ctx *api.Context, username, hashedPassword string) (uuid.UUID,
ExpiresAt: time.Now().Add(time.Hour), // TODO: decide on this one
}
if err := tx.Create(&accessToken).Error; err != nil {
return uuid.Nil, err
return nil, err
}

return person.ID, tx.Commit().Error
return &person, tx.Commit().Error
}

0 comments on commit 9960e54

Please sign in to comment.