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

Do ssh_auth #51

Merged
merged 2 commits into from
Jan 31, 2023
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
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