Skip to content

Commit 913aef1

Browse files
committed
Add cli cmd to get app security key
1 parent 62f676b commit 913aef1

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

cli/appsecuritykey.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package cli
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
7+
"github.com/coder/coder/cli/clibase"
8+
"github.com/coder/coder/coderd/database"
9+
"github.com/coder/coder/coderd/database/migrations"
10+
"golang.org/x/xerrors"
11+
)
12+
13+
func (*RootCmd) appSecurityKey() *clibase.Cmd {
14+
var postgresURL string
15+
16+
root := &clibase.Cmd{
17+
Use: "app-security-key",
18+
Short: "Directly connect to the database to print the app security key",
19+
// We can unhide this if we decide to keep it this way.
20+
Hidden: true,
21+
Middleware: clibase.RequireNArgs(0),
22+
Handler: func(inv *clibase.Invocation) error {
23+
sqlDB, err := sql.Open("postgres", postgresURL)
24+
if err != nil {
25+
return xerrors.Errorf("dial postgres: %w", err)
26+
}
27+
defer sqlDB.Close()
28+
err = sqlDB.Ping()
29+
if err != nil {
30+
return xerrors.Errorf("ping postgres: %w", err)
31+
}
32+
33+
err = migrations.EnsureClean(sqlDB)
34+
if err != nil {
35+
return xerrors.Errorf("database needs migration: %w", err)
36+
}
37+
db := database.New(sqlDB)
38+
39+
key, err := db.GetAppSecurityKey(inv.Context())
40+
if err != nil {
41+
return xerrors.Errorf("retrieving key: %w", err)
42+
}
43+
44+
_, _ = fmt.Fprintln(inv.Stdout, key)
45+
return nil
46+
},
47+
}
48+
49+
root.Options = clibase.OptionSet{
50+
{
51+
Flag: "postgres-url",
52+
Description: "URL of a PostgreSQL database to connect to.",
53+
Env: "CODER_PG_CONNECTION_URL",
54+
Value: clibase.StringOf(&postgresURL),
55+
},
56+
}
57+
58+
return root
59+
}

cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (r *RootCmd) Core() []*clibase.Cmd {
7979
r.portForward(),
8080
r.publickey(),
8181
r.resetPassword(),
82+
r.appSecurityKey(),
8283
r.state(),
8384
r.templates(),
8485
r.users(),

0 commit comments

Comments
 (0)