Skip to content

Commit

Permalink
client(bootstrap): bootstrap webhook secret on init (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Mar 19, 2020
1 parent 818dbde commit 1dc8d02
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 9 deletions.
3 changes: 2 additions & 1 deletion client/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func newIntegrationClient() *client.Client {
SSHPort: "69",
},
Daemon: &cfg.Daemon{
Port: "4303",
Port: "4303",
WebHookSecret: "sekret",
},
}
return client.NewClient(remote, client.Options{
Expand Down
6 changes: 3 additions & 3 deletions client/internal/compiled.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion client/scripts/daemon-up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set -e
DAEMON_RELEASE="%[1]s"
DAEMON_PORT="%[2]s"
HOST_ADDRESS="%[3]s"
WEBHOOK_SECRET="%[4]s"

# Inertia image details.
DAEMON_NAME=inertia-daemon
Expand Down Expand Up @@ -62,4 +63,4 @@ sudo docker run -d \
-e HOME="$HOME" \
-e SSH_KNOWN_HOSTS='/app/host/.ssh/known_hosts' \
--name "$DAEMON_NAME" \
"$IMAGE" "$HOST_ADDRESS" > /dev/null # 2>&1
"$IMAGE" "$HOST_ADDRESS --webhook.secret $WEBHOOK_SECRET" > /dev/null # 2>&1
2 changes: 1 addition & 1 deletion client/sshc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *SSHClient) DaemonUp() error {
return fmt.Errorf("could not initialize script: %w", err)
}
var daemonCmdStr = fmt.Sprintf(string(scriptBytes),
s.remote.Version, s.remote.Daemon.Port, s.remote.IP)
s.remote.Version, s.remote.Daemon.Port, s.remote.IP, s.remote.Daemon.WebHookSecret)
return s.ssh.RunStream(daemonCmdStr, false)
}

Expand Down
9 changes: 8 additions & 1 deletion client/sshc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestSSHClient_DaemonUp(t *testing.T) {
// Get original script for comparison
script, err := ioutil.ReadFile("scripts/daemon-up.sh")
assert.NoError(t, err)
actualCommand := fmt.Sprintf(string(script), "test", "4303", "127.0.0.1")
actualCommand := fmt.Sprintf(string(script), "test", "4303", "127.0.0.1", "")

// Get SSH runner
sshc, err := client.GetSSHClient()
Expand All @@ -68,6 +68,13 @@ func TestSSHClient_DaemonUp(t *testing.T) {
call, interact := session.RunStreamArgsForCall(0)
assert.False(t, interact)
assert.Equal(t, actualCommand, call)

// Check with WEBHOOK_SECRET provided, make sure the right command is run.
sshc.remote.Daemon.WebHookSecret = "sekret"
assert.NoError(t, sshc.DaemonUp())
call, interact = session.RunStreamArgsForCall(1)
assert.False(t, interact)
assert.Contains(t, call, "sekret")
}

func TestSSHClient_DaemonDown(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion daemon/inertiad/daemon/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ func (s *Server) upHandler(w http.ResponseWriter, r *http.Request) {
var gitOpts = upReq.GitOptions

// apply configuration updates
s.state.WebhookSecret = upReq.WebHookSecret
if upReq.WebHookSecret != "" {
s.state.WebhookSecret = upReq.WebHookSecret
}
s.deployment.SetConfig(project.DeploymentConfig{
ProjectName: upReq.Project,
BuildType: upReq.BuildType,
Expand Down
6 changes: 5 additions & 1 deletion daemon/inertiad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ var runCmd = &cobra.Command{
host address as an argument.
Example:
inertia daemon run 0.0.0.0 -p 8081`,
inertia daemon run 0.0.0.0 --port 8081`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
var conf = cfg.New()

// Init webhook secret
var webhookSecret, _ = cmd.Flags().GetString("webhook.secret")
conf.WebhookSecret = webhookSecret

// Set up deployment
var projectDatabasePath = path.Join(conf.DataDirectory, "project.db")
var projectDatabaseKeypath = path.Join(conf.SecretsDirectory, "db.key")
Expand Down

0 comments on commit 1dc8d02

Please sign in to comment.