Skip to content

Commit

Permalink
Merge pull request #29 from abjrcode/flow-manager
Browse files Browse the repository at this point in the history
add flow manager to connect providers with sinks
  • Loading branch information
abjrcode authored Dec 14, 2023
2 parents ca79c6d + 2566194 commit 2a92b94
Show file tree
Hide file tree
Showing 47 changed files with 674 additions and 356 deletions.
36 changes: 24 additions & 12 deletions app_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/abjrcode/swervo/internal/app"
"github.com/abjrcode/swervo/internal/plumbing"
"github.com/abjrcode/swervo/internal/utils"
awsidc "github.com/abjrcode/swervo/providers/aws_idc"
awscredentialsfile "github.com/abjrcode/swervo/sinks/aws_credentials_file"
Expand All @@ -32,10 +33,10 @@ type AppController struct {

awsIdcController *awsidc.AwsIdentityCenterController

awsCredentialsFileController *awscredentialsfile.AwsCredentialsFileController
awsCredentialsFileSinkController *awscredentialsfile.AwsCredentialsFileSinkController
}

func (c *AppController) Init(ctx context.Context, errorHandler app.ErrorHandler) {
func (c *AppController) init(ctx context.Context, errorHandler app.ErrorHandler) {
appMenu := menu.NewMenu()

logger := zerolog.Ctx(ctx).With().Str("component", "app_controller").Logger()
Expand Down Expand Up @@ -131,13 +132,13 @@ func (c *AppController) RunAppCommand(command string, commandInput map[string]an
Password: commandInput["password"].(string),
})
case "Auth_Lock":
c.authController.LockVault()
c.authController.LockVault(appContext)
case "Dashboard_ListProviders":
output = c.dashboardController.ListProviders()
case "Dashboard_ListCompatibleSinks":
output = c.dashboardController.ListCompatibleSinks(appContext, commandInput["providerCode"].(string))
case "Dashboard_ListFavorites":
output, err = c.dashboardController.ListFavorites(appContext)
case "Dashboard_ListSinks":
output = c.dashboardController.ListSinks()
case "AwsIdc_ListInstances":
output, err = c.awsIdcController.ListInstances(appContext)
case "AwsIdc_GetInstanceData":
Expand Down Expand Up @@ -182,17 +183,24 @@ func (c *AppController) RunAppCommand(command string, commandInput map[string]an
UserCode: commandInput["userCode"].(string),
DeviceCode: commandInput["deviceCode"].(string),
})
case "AwsCredentialsFile_ListInstances":
output, err = c.awsCredentialsFileController.ListInstances(appContext)
case "AwsCredentialsFile_NewInstance":
output, err = c.awsCredentialsFileController.NewInstance(appContext,
output, err = c.awsCredentialsFileSinkController.NewInstance(appContext,
awscredentialsfile.AwsCredentialsFile_NewInstanceCommandInput{
FilePath: commandInput["filePath"].(string),
Label: commandInput["label"].(string),
FilePath: commandInput["filePath"].(string),
AwsProfileName: commandInput["awsProfileName"].(string),
Label: commandInput["label"].(string),
ProviderCode: commandInput["providerCode"].(string),
ProviderId: commandInput["providerId"].(string),
})
case "AwsCredentialsFile_GetInstanceData":
output, err = c.awsCredentialsFileController.GetInstanceData(appContext,
commandInput["instanceId"].(string))
output, err = c.awsCredentialsFileSinkController.GetInstanceData(appContext,
commandInput["instanceId"].(string),
)
case "AwsCredentialsFile_DisconnectSink":
err = c.awsCredentialsFileSinkController.DisconnectSink(appContext, plumbing.DisconnectSinkCommandInput{
SinkCode: commandInput["sinkCode"].(string),
SinkId: commandInput["sinkId"].(string),
})
default:
output, err = nil, errors.Join(ErrInvalidAppCommand, app.ErrFatal)
}
Expand All @@ -201,5 +209,9 @@ func (c *AppController) RunAppCommand(command string, commandInput map[string]an
c.errorHandler.Catch(appContext, c.logger, err)
}

if errors.Is(err, app.ErrValidation) {
return output, errors.Unwrap(err)
}

return output, err
}
13 changes: 5 additions & 8 deletions auth_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import (

"github.com/abjrcode/swervo/internal/app"
"github.com/abjrcode/swervo/internal/security/vault"
"github.com/rs/zerolog"
)

type AuthController struct {
logger zerolog.Logger

vault vault.Vault
}

Expand All @@ -37,7 +34,7 @@ func check(err error) error {
}

func (c *AuthController) IsVaultConfigured(ctx app.Context) (bool, error) {
c.logger.Info().Msg("checking if vault is already configured")
ctx.Logger().Info().Msg("checking if vault is already configured")
isConfigured, err := c.vault.IsConfigured(ctx)

if check(err) != nil {
Expand All @@ -54,7 +51,7 @@ type Auth_ConfigureVaultCommandInput struct {
// ConfigureVault sets up the vault with a master password. It is called when the user sets up the app for the first time.
// After configuration, the vault is unsealed and ready to be used.
func (c *AuthController) ConfigureVault(ctx app.Context, input Auth_ConfigureVaultCommandInput) error {
c.logger.Info().Msg("setting up vault with a master password")
ctx.Logger().Info().Msg("setting up vault with a master password")
err := c.vault.Configure(ctx, input.Password)

if check(err) != nil {
Expand All @@ -70,7 +67,7 @@ type Auth_UnlockCommandInput struct {

// UnlockVault opens the vault with the given password. It is called when the user logs in.
func (c *AuthController) UnlockVault(ctx app.Context, input Auth_UnlockCommandInput) (bool, error) {
c.logger.Info().Msg("attempting to unlock vault with a master password")
ctx.Logger().Info().Msg("attempting to unlock vault with a master password")
success, err := c.vault.Open(ctx, input.Password)

if check(err) != nil {
Expand All @@ -81,7 +78,7 @@ func (c *AuthController) UnlockVault(ctx app.Context, input Auth_UnlockCommandIn
}

// LockVault closes the vault and purges the key from memory. It is called when the user logs out.
func (c *AuthController) LockVault() {
c.logger.Info().Msg("locking Vault")
func (c *AuthController) LockVault(ctx app.Context) {
ctx.Logger().Info().Msg("locking Vault")
c.vault.Seal()
}
7 changes: 4 additions & 3 deletions auth_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func TestAuthController_UnlockVault_WithWrongPassword(t *testing.T) {
err := controller.ConfigureVault(ctx, Auth_ConfigureVaultCommandInput{Password: "password"})
require.NoError(t, err)

controller.LockVault()
controller.LockVault(ctx)

mockTimeProvider.On("NowUnix").Return(2)
unlocked, err := controller.UnlockVault(ctx, Auth_UnlockCommandInput{Password: "wrong-password"})
Expand All @@ -99,8 +99,9 @@ func TestAuthController_UnlockVault_WithoutConfiguringFirst(t *testing.T) {

func TestAuthController_LockVault_WithoutConfiguringFirst(t *testing.T) {
controller, _ := initAuthController(t)
mockContext := testhelpers.NewMockAppContext()

controller.LockVault()
controller.LockVault(mockContext)
}

func TestAuthController_LockVault_WithoutUnlockingFirst(t *testing.T) {
Expand All @@ -111,7 +112,7 @@ func TestAuthController_LockVault_WithoutUnlockingFirst(t *testing.T) {
err := controller.ConfigureVault(ctx, Auth_ConfigureVaultCommandInput{Password: "password"})
require.NoError(t, err)

controller.LockVault()
controller.LockVault(ctx)
}

func TestAuthController_UnlockVault_WithoutLockingFirst(t *testing.T) {
Expand Down
27 changes: 14 additions & 13 deletions dashboard_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/abjrcode/swervo/favorites"
"github.com/abjrcode/swervo/internal/app"
"github.com/abjrcode/swervo/providers"
"github.com/abjrcode/swervo/sinks"
awsidc "github.com/abjrcode/swervo/providers/aws_idc"
)

type Provider struct {
Expand All @@ -20,10 +20,13 @@ type FavoriteInstance struct {
InstanceId string `json:"instanceId"`
}

type Sink struct {
Code string `json:"code"`
Name string `json:"name"`
IconSvgBase64 string `json:"iconSvgBase64"`
type CompatibleSink struct {
Code string `json:"code"`
Name string `json:"name"`
}

var ProviderCompatibleSinksMap = map[string][]CompatibleSink{
awsidc.ProviderCode: {},
}

type DashboardController struct {
Expand Down Expand Up @@ -69,14 +72,12 @@ func (c *DashboardController) ListProviders() []Provider {
return supportedProviders
}

func (c *DashboardController) ListSinks() []Sink {
supportedSinks := make([]Sink, 0, len(sinks.SupportedSinks))
for _, sink := range sinks.SupportedSinks {
supportedSinks = append(supportedSinks, Sink{
Code: sink.Code,
Name: sink.Name,
})
func (c *DashboardController) ListCompatibleSinks(ctx app.Context, providerCode string) []CompatibleSink {
sinkCodes, ok := ProviderCompatibleSinksMap[providerCode]

if !ok {
return []CompatibleSink{}
}

return supportedSinks
return sinkCodes
}
9 changes: 4 additions & 5 deletions favorites/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/abjrcode/swervo/internal/migrations"
"github.com/abjrcode/swervo/internal/testhelpers"
"github.com/abjrcode/swervo/providers"
"github.com/stretchr/testify/require"
)

Expand All @@ -16,7 +15,7 @@ func TestAddFavorite(t *testing.T) {
repo := NewFavorites(db)

favorite := &Favorite{
ProviderCode: providers.AwsIdc,
ProviderCode: "some-provider",
InstanceId: "some-nice-id",
}

Expand All @@ -39,7 +38,7 @@ func TestRemoveFavorite(t *testing.T) {
ctx := testhelpers.NewMockAppContext()

favorite := &Favorite{
ProviderCode: providers.AwsIdc,
ProviderCode: "some-provider",
InstanceId: "some-nice-id",
}

Expand Down Expand Up @@ -69,7 +68,7 @@ func TestIsFavorite(t *testing.T) {
ctx := testhelpers.NewMockAppContext()

favorite := &Favorite{
ProviderCode: providers.AwsIdc,
ProviderCode: "some-provider",
InstanceId: "some-nice-id",
}

Expand All @@ -81,7 +80,7 @@ func TestIsFavorite(t *testing.T) {
require.True(t, isFavorite)

isFavorite, err = repo.IsFavorite(ctx, &Favorite{
ProviderCode: providers.AwsIdc,
ProviderCode: "some-provider",
InstanceId: "some-nice-id-2",
})
require.NoError(t, err)
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/components/sink-component-map.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AwsCredentialsFile } from "../routes/sinks/aws-credentials-file/card"
import { SinkCodes } from "../utils/provider-sink-codes"

export function createSinkCard(sinkCode: string, sinkId: string) {
switch (sinkCode) {
case SinkCodes.AwsCredentialsFile:
return <AwsCredentialsFile instanceId={sinkId} />

default:
throw new Error("Unknown sinkCode: " + sinkCode)
}
}
9 changes: 2 additions & 7 deletions frontend/src/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useEffect, useState } from "react"
import { useToaster } from "./toast-provider/toast-context"
import { useAuth } from "./auth-provider/auth-context"
import { LockVault } from "../wailsjs/go/main/AuthController"
import { Link, useNavigate } from "react-router-dom"
import { Auth_Lock } from "./utils/ipc-adapter"

export function Layout({ children }: { children: React.ReactNode }) {
const toaster = useToaster()
Expand All @@ -20,7 +20,7 @@ export function Layout({ children }: { children: React.ReactNode }) {
}, [toaster])

async function attemptLock() {
await LockVault()
await Auth_Lock()

navigate("/")
authContext.onVaultLocked()
Expand Down Expand Up @@ -57,11 +57,6 @@ export function Layout({ children }: { children: React.ReactNode }) {
className="btn btn-primary btn-outline capitalize">
providers
</Link>
<Link
to="/sinks"
className="btn btn-primary btn-outline capitalize">
sinks
</Link>
</nav>

<button
Expand Down
30 changes: 12 additions & 18 deletions frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ import { ToastProvider } from "./toast-provider/toast-provider"
import { AwsIdcInstances } from "./routes/providers/aws-idc/instances"
import { awsIdcInstancesData } from "./routes/providers/aws-idc/instances-data"
import { Auth_IsVaultConfigured } from "./utils/ipc-adapter"
import { Sinks } from "./routes/sinks/sinks"
import { sinksLoader } from "./routes/sinks/sinks-data"
import { AwsCredentialsFileInstances } from "./routes/sinks/aws-credentials-file/instances"
import { awsCredentialsFileInstancesData } from "./routes/sinks/aws-credentials-file/instances-data"
import { AwsCredentialsFileNew } from "./routes/sinks/aws-credentials-file/new"
import { CompatibleSinks } from "./routes/sinks/compatible-sinks"
import { AwsCredentialsFileSetup } from "./routes/sinks/aws-credentials-file/new"
import { awsCredentialsFileSetupAction } from "./routes/sinks/aws-credentials-file/new-data"
import { awsCredentialsFileCardLoader } from "./routes/sinks/aws-credentials-file/card-data"
import { ProviderCodes, SinkCodes } from "./utils/provider-sink-codes"
import { compatibleSinksLoader } from "./routes/sinks/compatible-sinks-data"

const devMode = import.meta.env.DEV

Expand All @@ -54,7 +53,7 @@ void (async function main() {
loader: providersLoader,
children: [
{
path: "aws-idc",
path: ProviderCodes.AwsIdc,
children: [
{
index: true,
Expand All @@ -77,20 +76,15 @@ void (async function main() {
},
{
path: "/sinks",
element: <Sinks />,
loader: sinksLoader,
element: <CompatibleSinks />,
loader: compatibleSinksLoader,
children: [
{
path: "aws-credentials-file",
path: SinkCodes.AwsCredentialsFile,
children: [
{
index: true,
element: <AwsCredentialsFileInstances />,
loader: awsCredentialsFileInstancesData,
},
{
path: "setup",
element: <AwsCredentialsFileNew />,
path: "setup/:providerCode/:instanceId",
element: <AwsCredentialsFileSetup />,
action: awsCredentialsFileSetupAction,
},
],
Expand All @@ -106,11 +100,11 @@ void (async function main() {
path: "api",
children: [
{
path: "aws-idc-card",
path: `${ProviderCodes.AwsIdc}-card`,
loader: awsIdcCardLoader,
},
{
path: "aws-credentials-file-card",
path: `${SinkCodes.AwsCredentialsFile}-card`,
loader: awsCredentialsFileCardLoader,
},
],
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/routes/dashboard/dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useLoaderData } from "react-router-dom"
import { AwsIdcCard } from "../providers/aws-idc/card"
import { main } from "../../../wailsjs/go/models"
import { ProviderCodes } from "../../utils/provider-sink-codes"

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const ProviderComponentMap = new Map<string, React.FC<any>>([
["aws-idc", AwsIdcCard],
[ProviderCodes.AwsIdc, AwsIdcCard],
])

export function Dashboard() {
Expand Down
Loading

0 comments on commit 2a92b94

Please sign in to comment.