-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* draft: agent create api [skip ci] * chore: rbac integration and validation of access token [skip ci] * chore: remove agent/controllers_test.go [skip ci] * impl: bcrypt [skip ci] * refactor: separate generateAgent func [skip ci] * chore: bump duty * chore: address review comment * feat: use argon2 instead of bcrypt argon2 allows us to supply the salt whereas bcrypt doesn't. * fix: uint parsing * Removed rand & hash utils because it's in commons * chore: use base64.URLEncoding * feat: access token cache and better errors for the user * chore: lint fix * chore: bump commons and removed some utils that are in commons * chore: create and save a dummy email for agent person * feat: only allow admins to generate new agents
- Loading branch information
1 parent
7df19bf
commit a9f7d84
Showing
18 changed files
with
342 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package agent | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/flanksource/commons/rand" | ||
"github.com/flanksource/incident-commander/api" | ||
"github.com/flanksource/incident-commander/db" | ||
"github.com/flanksource/incident-commander/rbac" | ||
) | ||
|
||
// 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 { | ||
return nil, fmt.Errorf("failed to generate username and password: %w", err) | ||
} | ||
|
||
person, err := db.CreatePerson(ctx, username, fmt.Sprintf("%s@local", username), "agent") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create a new person: %w", err) | ||
} | ||
|
||
token, err := db.CreateAccessToken(ctx, person.ID, "default", password, time.Hour*24*365) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create a new access token: %w", err) | ||
} | ||
|
||
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, &person.ID, body.Properties); err != nil { | ||
return nil, fmt.Errorf("failed to create a new agent: %w", err) | ||
} | ||
|
||
return &api.GeneratedAgent{ | ||
ID: person.ID.String(), | ||
Username: username, | ||
AccessToken: token, | ||
}, nil | ||
} | ||
|
||
// genUsernamePassword generates a random pair of username and password | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package agent | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/flanksource/commons/logger" | ||
"github.com/flanksource/incident-commander/api" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
// GenerateAgent creates a new person and a new agent and associates them. | ||
func GenerateAgent(c echo.Context) error { | ||
ctx := c.(*api.Context) | ||
|
||
var body api.GenerateAgentRequest | ||
if err := json.NewDecoder(c.Request().Body).Decode(&body); err != nil { | ||
return c.JSON(http.StatusBadRequest, api.HTTPError{Error: err.Error()}) | ||
} | ||
|
||
agent, err := generateAgent(ctx, body) | ||
if err != nil { | ||
logger.Errorf("failed to generate a new agent: %v", err) | ||
return c.JSON(http.StatusInternalServerError, api.HTTPError{Error: err.Error(), Message: "error generating agent"}) | ||
} | ||
|
||
return c.JSON(http.StatusCreated, agent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package api | ||
|
||
type GenerateAgentRequest struct { | ||
Name string | ||
Properties map[string]string | ||
} | ||
|
||
type GeneratedAgent struct { | ||
ID string `json:"id"` | ||
Username string `json:"username"` | ||
AccessToken string `json:"access_token"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.