-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c367e53
commit f7bcc7e
Showing
25 changed files
with
1,077 additions
and
360 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,66 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"github.com/anchordotdev/cli" | ||
"github.com/anchordotdev/cli/api" | ||
"github.com/anchordotdev/cli/auth/models" | ||
"github.com/anchordotdev/cli/ui" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
type Client struct { | ||
Config *cli.Config | ||
|
||
Anc *api.Session | ||
Hint tea.Model | ||
Source string | ||
} | ||
|
||
func (c Client) Perform(ctx context.Context, drv *ui.Driver) (*api.Session, error) { | ||
var newClientErr, userInfoErr error | ||
|
||
drv.Activate(ctx, &models.Client{}) | ||
|
||
if c.Anc == nil { | ||
c.Anc, newClientErr = api.NewClient(c.Config) | ||
if newClientErr != nil && !errors.Is(newClientErr, api.ErrSignedOut) { | ||
return nil, newClientErr | ||
} | ||
} | ||
|
||
drv.Send(models.ClientProbed(true)) | ||
|
||
if newClientErr == nil { | ||
_, userInfoErr := c.Anc.UserInfo(ctx) | ||
if userInfoErr != nil && !errors.Is(userInfoErr, api.ErrSignedOut) { | ||
return nil, userInfoErr | ||
} | ||
} | ||
|
||
drv.Send(models.ClientTested(true)) | ||
|
||
if errors.Is(newClientErr, api.ErrSignedOut) || errors.Is(userInfoErr, api.ErrSignedOut) { | ||
if c.Hint == nil { | ||
c.Hint = &models.SignInHint{} | ||
} | ||
cmd := &SignIn{ | ||
Config: c.Config, | ||
Hint: c.Hint, | ||
Source: c.Source, | ||
} | ||
err := cmd.RunTUI(ctx, drv) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
c.Anc, err = api.NewClient(c.Config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
return c.Anc, 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,59 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/anchordotdev/cli/ui" | ||
"github.com/charmbracelet/bubbles/spinner" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
type AuditAuthenticationWhoami string | ||
|
||
type ClientProbed bool | ||
type ClientTested bool | ||
|
||
type Client struct { | ||
spinner spinner.Model | ||
|
||
probed bool | ||
tested bool | ||
} | ||
|
||
func (m *Client) Init() tea.Cmd { | ||
m.spinner = ui.WaitingSpinner() | ||
|
||
return m.spinner.Tick | ||
} | ||
|
||
func (m *Client) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case ClientProbed: | ||
m.probed = bool(msg) | ||
return m, nil | ||
case ClientTested: | ||
m.tested = bool(msg) | ||
return m, nil | ||
default: | ||
var cmd tea.Cmd | ||
m.spinner, cmd = m.spinner.Update(msg) | ||
return m, cmd | ||
} | ||
} | ||
|
||
func (m *Client) View() string { | ||
var b strings.Builder | ||
|
||
if !m.probed { | ||
fmt.Fprintln(&b, ui.StepInProgress(fmt.Sprintf("Checking authentication: probing credentials locally…%s", m.spinner.View()))) | ||
return b.String() | ||
} | ||
|
||
if !m.tested { | ||
fmt.Fprintln(&b, ui.StepInProgress(fmt.Sprintf("Checking authentication: testing credentials remotely…%s", m.spinner.View()))) | ||
return b.String() | ||
} | ||
|
||
return b.String() | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package lcl | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/anchordotdev/cli" | ||
"github.com/anchordotdev/cli/api" | ||
"github.com/anchordotdev/cli/auth" | ||
"github.com/anchordotdev/cli/lcl/models" | ||
"github.com/anchordotdev/cli/trust" | ||
"github.com/anchordotdev/cli/ui" | ||
) | ||
|
||
type Audit struct { | ||
Config *cli.Config | ||
|
||
anc *api.Session | ||
orgSlug, realmSlug string | ||
} | ||
|
||
func (c Audit) UI() cli.UI { | ||
return cli.UI{ | ||
RunTUI: c.run, | ||
} | ||
} | ||
|
||
func (c Audit) run(ctx context.Context, drv *ui.Driver) error { | ||
var err error | ||
cmd := &auth.Client{ | ||
Config: c.Config, | ||
Anc: c.anc, | ||
Source: "lclhost", | ||
} | ||
c.anc, err = cmd.Perform(ctx, drv) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
drv.Activate(ctx, &models.AuditHeader{}) | ||
drv.Activate(ctx, &models.AuditHint{}) | ||
|
||
_, err = c.perform(ctx, drv) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type LclAuditResult struct { | ||
diagnosticServiceExists, trusted bool | ||
} | ||
|
||
func (c Audit) perform(ctx context.Context, drv *ui.Driver) (*LclAuditResult, error) { | ||
var result = LclAuditResult{} | ||
|
||
drv.Activate(ctx, &models.AuditResources{}) | ||
|
||
if c.orgSlug == "" { | ||
userInfo, err := c.anc.UserInfo(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
c.orgSlug = userInfo.PersonalOrg.Slug | ||
} | ||
|
||
if c.realmSlug == "" { | ||
c.realmSlug = "localhost" | ||
} | ||
|
||
var diagnosticService *api.Service | ||
services, err := c.anc.GetOrgServices(ctx, c.orgSlug) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, service := range services { | ||
if service.ServerType == "diagnostic" { | ||
diagnosticService = &service | ||
} | ||
} | ||
|
||
if diagnosticService == nil { | ||
drv.Send(models.AuditResourcesNotFoundMsg{}) | ||
} else { | ||
result.diagnosticServiceExists = true | ||
drv.Send(models.AuditResourcesFoundMsg{}) | ||
} | ||
|
||
drv.Activate(ctx, &models.AuditTrust{}) | ||
|
||
// FIXME: use config anc? | ||
trustStoreAuditResult, err := trust.PerformAudit(ctx, c.Config, c.anc, c.orgSlug, c.realmSlug) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
missingCount := len(trustStoreAuditResult.Missing) | ||
result.trusted = missingCount == 0 | ||
drv.Send(models.AuditTrustMissingMsg(missingCount)) | ||
|
||
// TODO: audit local app status | ||
|
||
return &result, nil | ||
} |
Oops, something went wrong.