Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: refactor passphrase generateor + fix admin redump #589

Merged
merged 2 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions go/pkg/pwagent/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package pwagent

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

"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"go.uber.org/multierr"
"go.uber.org/zap"
"pathwar.land/pathwar/v2/go/internal/randstring"
"pathwar.land/pathwar/v2/go/pkg/errcode"
"pathwar.land/pathwar/v2/go/pkg/pwapi"
"pathwar.land/pathwar/v2/go/pkg/pwcompose"
"pathwar.land/pathwar/v2/go/pkg/pwdb"
"pathwar.land/pathwar/v2/go/pkg/pwinit"
)

func applyDockerConfig(ctx context.Context, apiInstances *pwapi.AgentListInstances_Output, dockerClient *client.Client, opts Opts) error {
Expand Down Expand Up @@ -81,10 +84,11 @@ func applyDockerConfig(ctx context.Context, apiInstances *pwapi.AgentListInstanc
}

// parse pwinit config
configData, err := instance.ParseInstanceConfig()
if err != nil {
errs = multierr.Append(errs, err)
continue
configData := pwinit.InitConfig{
Passphrases: make([]string, instance.Flavor.Passphrases),
}
for i := 0; i < int(instance.Flavor.Passphrases); i++ {
configData.Passphrases[i] = randstring.RandString(14)
}

bundle := instance.GetFlavor().GetComposeBundle()
Expand All @@ -96,7 +100,7 @@ func applyDockerConfig(ctx context.Context, apiInstances *pwapi.AgentListInstanc
InstanceKey: instanceID, // WARN -> normal?
ForceRecreate: true,
ProxyNetworkID: proxyNetworkID,
PwinitConfig: configData,
PwinitConfig: &configData,
Logger: opts.Logger,
}
containers, err := pwcompose.Up(ctx, dockerClient, upOpts)
Expand All @@ -105,6 +109,13 @@ func applyDockerConfig(ctx context.Context, apiInstances *pwapi.AgentListInstanc
continue
}

out, err := json.Marshal(configData)
if err != nil {
errs = multierr.Append(errs, errcode.TODO.Wrap(err))
continue
}
instance.InstanceConfig = out

l.Info(
"started instance",
zap.Duration("duration", time.Since(before)),
Expand Down
7 changes: 3 additions & 4 deletions go/pkg/pwapi/api_admin-challenge-flavor-add.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,9 @@ func (svc *service) AdminChallengeFlavorAdd(ctx context.Context, in *AdminChalle

for _, agent := range agentsToInstanciate {
instance := pwdb.ChallengeInstance{
Status: pwdb.ChallengeInstance_IsNew,
AgentID: agent.ID,
FlavorID: in.ChallengeFlavor.ID,
InstanceConfig: []byte(`{"passphrases": ["a", "b", "c", "d"]}`),
Status: pwdb.ChallengeInstance_IsNew,
AgentID: agent.ID,
FlavorID: in.ChallengeFlavor.ID,
}
err = tx.Create(&instance).Error
if err != nil {
Expand Down
47 changes: 26 additions & 21 deletions go/pkg/pwapi/api_admin-redump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package pwapi

import (
"context"
"strconv"
"fmt"

"go.uber.org/zap"
"go.uber.org/multierr"
"pathwar.land/pathwar/v2/go/pkg/errcode"
"pathwar.land/pathwar/v2/go/pkg/pwdb"
)
Expand All @@ -14,32 +14,37 @@ func (svc *service) AdminRedump(ctx context.Context, in *AdminRedump_Input) (*Ad
return nil, errcode.ErrRestrictedArea
}

var errs error
for _, identifier := range in.Identifiers {
nb, err := strconv.Atoi(identifier)
if err != nil {
// for now, we only accept numerical identifiers, but the plan is to also search for names
return nil, errcode.TODO.Wrap(err)
instances := []int64{}
if id, err := pwdb.GetIDBySlugAndKind(svc.db, identifier, "challenge-instance"); err == nil {
instances = append(instances, id)
} else if id, err := pwdb.GetIDBySlugAndKind(svc.db, identifier, "challenge-flavor"); err == nil {
var flavor pwdb.ChallengeFlavor
if err = svc.db.Preload("Instances").First(&flavor, id).Error; err != nil {
errs = multierr.Append(errs, err)
continue
}
for _, instance := range flavor.Instances {
instances = append(instances, instance.ID)
}
}

// FIXME: support passing IDs for other entities too
var instance pwdb.ChallengeInstance
err = svc.db.First(&instance, nb).Error
if err != nil {
return nil, errcode.TODO.Wrap(err)
if len(instances) == 0 {
errs = multierr.Append(errs, fmt.Errorf("no such entry for %q", identifier))
continue
}

switch instance.Status {
case pwdb.ChallengeInstance_NeedRedump:
svc.logger.Debug("level already marked as needing a redump", zap.Int64("instance-id", instance.ID))
default:
err = svc.db.Model(&instance).Updates(&pwdb.ChallengeInstance{Status: pwdb.ChallengeInstance_NeedRedump}).Error
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
svc.logger.Debug("level marked as needing a redump", zap.Int64("instance-id", instance.ID))
err := svc.db.
Model(pwdb.ChallengeInstance{}).
Where("id IN (?)", instances).
Updates(&pwdb.ChallengeInstance{Status: pwdb.ChallengeInstance_NeedRedump}).
Error
if err != nil {
errs = multierr.Append(errs, err)
}
}

out := AdminRedump_Output{}
return &out, nil
return &out, errs
}
7 changes: 3 additions & 4 deletions go/pkg/pwapi/api_agent-register.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,9 @@ func (svc *service) AgentRegister(ctx context.Context, in *AgentRegister_Input)

for _, challengeFlavor := range challengeFlavorsToInstanciate {
instance := pwdb.ChallengeInstance{
Status: pwdb.ChallengeInstance_IsNew,
AgentID: agent.ID,
FlavorID: challengeFlavor.ID,
InstanceConfig: []byte(`{"passphrases": ["a", "b", "c", "d"]}`),
Status: pwdb.ChallengeInstance_IsNew,
AgentID: agent.ID,
FlavorID: challengeFlavor.ID,
}
err = svc.db.Create(&instance).Error
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion go/pkg/pwapi/api_agent-update-state.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func (svc *service) AgentUpdateState(ctx context.Context, in *AgentUpdateState_I
cpy := challengeInstance
err := svc.db.Model(&cpy).
Update(pwdb.ChallengeInstance{
Status: challengeInstance.Status,
Status: challengeInstance.Status,
InstanceConfig: challengeInstance.InstanceConfig,
}).
Error
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions go/pkg/pwdb/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ func GetIDBySlugAndKind(db *gorm.DB, slug string, kind string) (int64, error) {
Model(ChallengeFlavor{}).
Where("id = ? OR slug = ? OR slug = ?", slug, slug, slug+"@default").
Pluck("id", &ids).Error
case "challenge-instance":
err = db.
Model(ChallengeInstance{}).
Where("id = ?", slug).
Pluck("id", &ids).Error
default:
return 0, errcode.ErrUnknownDBKind
}
Expand Down