Skip to content

Commit

Permalink
Merge pull request #51 from fly-apps/ssh_auth
Browse files Browse the repository at this point in the history
Do ssh_auth
  • Loading branch information
DAlperin authored Jan 31, 2023
2 parents 1ab7d9d + a219eb4 commit 87dfce7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ LABEL fly.pg-version=${PG_VERSION}
LABEL fly.pg-manager=repmgr

RUN apt-get update && apt-get install --no-install-recommends -y \
ca-certificates iproute2 postgresql-$PG_MAJOR_VERSION-repmgr curl bash dnsutils vim procps jq pgbouncer \
ca-certificates iproute2 postgresql-$PG_MAJOR_VERSION-repmgr curl bash dnsutils vim procps jq pgbouncer ssh \
&& apt autoremove -y

COPY --from=0 /fly/bin/* /usr/local/bin
Expand Down
50 changes: 50 additions & 0 deletions pkg/flypg/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ func (n *Node) Init(ctx context.Context) error {
}
}

err := WriteSSHKey()
if err != nil {
return fmt.Errorf("failed initialize ssh. %v", err)
}

store, err := state.NewStore()
if err != nil {
return fmt.Errorf("failed initialize cluster state store: %s", err)
Expand Down Expand Up @@ -240,6 +245,44 @@ func (n *Node) Init(ctx context.Context) error {
return nil
}

func WriteSSHKey() error {
err := os.Mkdir("/data/.ssh", 0700)
if err != nil && !os.IsExist(err) {
return err
}

key := os.Getenv("SSH_KEY")

keyFile, err := os.Create("/data/.ssh/id_rsa")
if err != nil {
return err
}
defer keyFile.Close()
_, err = keyFile.Write([]byte(key))
if err != nil {
return err
}

cert := os.Getenv("SSH_CERT")

certFile, err := os.Create("/data/.ssh/id_rsa-cert.pub")
if err != nil {
return err
}
defer certFile.Close()
_, err = certFile.Write([]byte(cert))
if err != nil {
return err
}

err = setSSHOwnership()
if err != nil {
return err
}

return nil
}

// PostInit are operations that should be executed against a running Postgres on boot.
func (n *Node) PostInit(ctx context.Context) error {
if ZombieLockExists() {
Expand Down Expand Up @@ -702,6 +745,13 @@ func openConnection(parentCtx context.Context, host string, database string, cre
return pgx.ConnectConfig(ctx, conf)
}

func setSSHOwnership() error {
cmdStr := fmt.Sprintf("chmod 600 %s %s", "/data/.ssh/id_rsa", "/data/.ssh/id_rsa-cert.pub")
cmd := exec.Command("sh", "-c", cmdStr)
_, err := cmd.Output()
return err
}

func setDirOwnership() error {
pgUser, err := user.Lookup("postgres")
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/flypg/repmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func (r *RepMgr) setDefaults() {
"location": r.Region,
"primary_visibility_consensus": true,
"failover_validation_command": fmt.Sprintf("'/usr/local/bin/failover_validation -visible-nodes %%v -total-nodes %%t'"),
"ssh_options": "'-o \"StrictHostKeyChecking=no\"'",
"priority": 100,
}

Expand Down

0 comments on commit 87dfce7

Please sign in to comment.