Skip to content

Commit

Permalink
dbaas: use dedicated reveal-password endpoint to fetch password and b…
Browse files Browse the repository at this point in the history
…uild URI
  • Loading branch information
kobajagi committed Jul 24, 2024
1 parent ce58594 commit 89bc3cd
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 4 deletions.
35 changes: 34 additions & 1 deletion cmd/dbaas_show_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/egoscale/v2/oapi"
v3 "github.com/exoscale/egoscale/v3"
)

type dbServiceMysqlComponentShowOutput struct {
Expand Down Expand Up @@ -147,7 +148,39 @@ func (c *dbaasServiceShowCmd) showDatabaseServiceMysql(ctx context.Context) (out
return nil, nil

case c.ShowURI:
fmt.Println(utils.DefaultString(databaseService.Uri, ""))
// Read password from dedicated endpoint
client, err := switchClientZoneV3(
ctx,
globalstate.EgoscaleV3Client,
v3.ZoneName(c.Zone),
)
if err != nil {
return nil, err
}

uriParams := *databaseService.UriParams

creds, err := client.RevealDBAASMysqlUserPassword(
ctx,
string(databaseService.Name),
uriParams["user"].(string),
)
if err != nil {
return nil, err
}

// Build URI
uri := fmt.Sprintf(
"mysql://%s:%s@%s:%s/%s?ssl-mode=%s",
uriParams["user"],
creds.Password,
uriParams["host"],
uriParams["port"],
uriParams["dbname"],
uriParams["ssl-mode"],
)

fmt.Println(uri)
return nil, nil
}

Expand Down
33 changes: 32 additions & 1 deletion cmd/dbaas_show_opensearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/egoscale/v2/oapi"
v3 "github.com/exoscale/egoscale/v3"
)

type dbServiceOpensearchComponentsShowOutput struct {
Expand Down Expand Up @@ -147,7 +148,37 @@ func (c *dbaasServiceShowCmd) showDatabaseServiceOpensearch(ctx context.Context)
case c.ShowSettings != "":
return nil, opensearchShowSettings(c.ShowSettings, res.JSON200)
case c.ShowURI:
fmt.Println(utils.DefaultString(res.JSON200.Uri, ""))
// Read password from dedicated endpoint
client, err := switchClientZoneV3(
ctx,
globalstate.EgoscaleV3Client,
v3.ZoneName(c.Zone),
)
if err != nil {
return nil, err
}

uriParams := *res.JSON200.UriParams

creds, err := client.RevealDBAASOpensearchUserPassword(
ctx,
string(res.JSON200.Name),
uriParams["user"].(string),
)
if err != nil {
return nil, err
}

// Build URI
uri := fmt.Sprintf(
"https://%s:%s@%s:%s",
uriParams["user"],
creds.Password,
uriParams["host"],
uriParams["port"],
)

fmt.Println(uri)
return nil, nil
default:
return opensearchShowDatabase(res.JSON200, c.Zone)
Expand Down
35 changes: 34 additions & 1 deletion cmd/dbaas_show_pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/egoscale/v2/oapi"
v3 "github.com/exoscale/egoscale/v3"
)

type dbServicePGConnectionPool struct {
Expand Down Expand Up @@ -181,7 +182,39 @@ func (c *dbaasServiceShowCmd) showDatabaseServicePG(ctx context.Context) (output
return nil, nil

case c.ShowURI:
fmt.Println(utils.DefaultString(databaseService.Uri, ""))
// Read password from dedicated endpoint
client, err := switchClientZoneV3(
ctx,
globalstate.EgoscaleV3Client,
v3.ZoneName(c.Zone),
)
if err != nil {
return nil, err
}

uriParams := *databaseService.UriParams

creds, err := client.RevealDBAASPostgresUserPassword(
ctx,
string(databaseService.Name),
uriParams["user"].(string),
)
if err != nil {
return nil, err
}

// Build URI
uri := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s?sslmode=%s",
uriParams["user"],
creds.Password,
uriParams["host"],
uriParams["port"],
uriParams["dbname"],
uriParams["sslmode"],
)

fmt.Println(uri)
return nil, nil
}

Expand Down
33 changes: 32 additions & 1 deletion cmd/dbaas_show_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/exoscale/cli/utils"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/egoscale/v2/oapi"
v3 "github.com/exoscale/egoscale/v3"
)

type dbServiceRedisComponentShowOutput struct {
Expand Down Expand Up @@ -143,7 +144,37 @@ func (c *dbaasServiceShowCmd) showDatabaseServiceRedis(ctx context.Context) (out
return nil, nil

case c.ShowURI:
fmt.Println(utils.DefaultString(databaseService.Uri, ""))
// Read password from dedicated endpoint
client, err := switchClientZoneV3(
ctx,
globalstate.EgoscaleV3Client,
v3.ZoneName(c.Zone),
)
if err != nil {
return nil, err
}

uriParams := *databaseService.UriParams

creds, err := client.RevealDBAASRedisUserPassword(
ctx,
string(databaseService.Name),
uriParams["user"].(string),
)
if err != nil {
return nil, err
}

// Build URI
uri := fmt.Sprintf(
"rediss://%s:%s@%s:%s",
uriParams["user"],
creds.Password,
uriParams["host"],
uriParams["port"],
)

fmt.Println(uri)
return nil, nil
}

Expand Down

0 comments on commit 89bc3cd

Please sign in to comment.