Skip to content

Commit

Permalink
feat: pathwar compose up injects a config file that contains cha… (pa…
Browse files Browse the repository at this point in the history
…thwar#302)

feat: pathwar compose up injects a config file that contains challeng…
  • Loading branch information
moul authored Dec 9, 2019
2 parents bcf60dd + 9a2bac4 commit 166c4be
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 142 deletions.
9 changes: 6 additions & 3 deletions api/errcode.proto
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ enum ErrCode {
ErrGetPWInitBinary = 3017;
ErrWritePWInitFileHeader = 3018;
ErrWritePWInitFile = 3019;
ErrWritePWInitCloseTarWriter = 3020;
ErrCopyPWInitToContainer = 3021;
ErrComposeGetPathwarInfo = 3022;
ErrMarshalPWInitConfigFile = 3020;
ErrWritePWInitConfigFileHeader = 3021;
ErrWritePWInitConfigFile = 3022;
ErrWritePWInitCloseTarWriter = 3023;
ErrCopyPWInitToContainer = 3024;
ErrComposeGetPathwarInfo = 3025;


//// Pathwar API (starting at 4001)
Expand Down
2 changes: 1 addition & 1 deletion docs/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion go/cmd/pathwar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func main() {
return errcode.ErrInitDockerClient.Wrap(err)
}

return pwcompose.Up(ctx, string(preparedCompose), composeUpInstanceKey, composeUpForceRecreate, cli, logger)
return pwcompose.Up(ctx, string(preparedCompose), composeUpInstanceKey, composeUpForceRecreate, nil, cli, logger)
},
}

Expand Down
2 changes: 1 addition & 1 deletion go/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

272 changes: 141 additions & 131 deletions go/pkg/errcode/errcode.pb.go

Large diffs are not rendered by default.

48 changes: 43 additions & 5 deletions go/pkg/pwcompose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/olekukonko/tablewriter"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
"pathwar.land/go/internal/randstring"
"pathwar.land/go/pkg/errcode"
"pathwar.land/go/pkg/pwinit"
)
Expand Down Expand Up @@ -177,6 +178,7 @@ func Up(
preparedCompose string,
instanceKey string,
forceRecreate bool,
pwinitConfigData *pwInitConfig,
cli *client.Client,
logger *zap.Logger,
) error {
Expand Down Expand Up @@ -257,7 +259,23 @@ func Up(

for _, container := range pwInfo.RunningInstances {
if challengeID == challengeIDFormatted(container.Labels[challengeNameLabel], container.Labels[challengeVersionLabel]) {
buf, err := buildPWInitTar()
if pwinitConfigData == nil {
pwinitConfigData = &pwInitConfig{
Passphrases: []string{
fmt.Sprintf("dev-%s", randstring.RandString(10)),
fmt.Sprintf("dev-%s", randstring.RandString(10)),
fmt.Sprintf("dev-%s", randstring.RandString(10)),
fmt.Sprintf("dev-%s", randstring.RandString(10)),
fmt.Sprintf("dev-%s", randstring.RandString(10)),
fmt.Sprintf("dev-%s", randstring.RandString(10)),
fmt.Sprintf("dev-%s", randstring.RandString(10)),
fmt.Sprintf("dev-%s", randstring.RandString(10)),
fmt.Sprintf("dev-%s", randstring.RandString(10)),
fmt.Sprintf("dev-%s", randstring.RandString(10)),
},
}
}
buf, err := buildPWInitTar(*pwinitConfigData)
if err != nil {
return errcode.ErrCopyPWInitToContainer.Wrap(err)
}
Expand Down Expand Up @@ -396,7 +414,7 @@ func PS(ctx context.Context, depth int, cli *client.Client, logger *zap.Logger)
return nil
}

func buildPWInitTar() (*bytes.Buffer, error) {
func buildPWInitTar(pwinitConfigData pwInitConfig) (*bytes.Buffer, error) {
var pwInitBuf []byte
pwInitBuf, err := pwinit.Binary()
if err != nil {
Expand All @@ -405,6 +423,8 @@ func buildPWInitTar() (*bytes.Buffer, error) {

var buf bytes.Buffer
tw := tar.NewWriter(&buf)

// write pwinit binary into tar file
err = tw.WriteHeader(&tar.Header{
Name: "pwinit",
Mode: 0755,
Expand All @@ -413,13 +433,31 @@ func buildPWInitTar() (*bytes.Buffer, error) {
if err != nil {
return nil, errcode.ErrWritePWInitFileHeader.Wrap(err)
}

if _, err := tw.Write(pwInitBuf); err != nil {
_, err = tw.Write(pwInitBuf)
if err != nil {
return nil, errcode.ErrWritePWInitFile.Wrap(err)
}

err = tw.Close()
// write pwinit json config into tar file
pwInitConfigJSON, err := json.Marshal(pwinitConfigData)
if err != nil {
return nil, errcode.ErrMarshalPWInitConfigFile.Wrap(err)
}
err = tw.WriteHeader(&tar.Header{
Name: "pwinit.json",
Mode: 0755,
Size: int64(len(pwInitConfigJSON)),
// FIXME: chown it to container's default user
})
if err != nil {
return nil, errcode.ErrWritePWInitConfigFileHeader.Wrap(err)
}
_, err = tw.Write(pwInitConfigJSON)
if err != nil {
return nil, errcode.ErrWritePWInitConfigFile.Wrap(err)
}

if err = tw.Close(); err != nil {
return nil, errcode.ErrWritePWInitCloseTarWriter.Wrap(err)
}

Expand Down
4 changes: 4 additions & 0 deletions go/pkg/pwcompose/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ type challengeFlavors struct {
Version string
Instances map[string]types.Container
}

type pwInitConfig struct {
Passphrases []string
}

0 comments on commit 166c4be

Please sign in to comment.