Skip to content

Commit

Permalink
alpha: dbproxy with user, insights for cloud sql, error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
23doors committed Apr 12, 2022
1 parent 7541cdf commit 23845d2
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 42 deletions.
4 changes: 4 additions & 0 deletions deploy/app_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ func (o *ServiceApp) Plan(ctx context.Context, pctx *config.PluginContext, r *re
return err
}

if !o.Image.IsExisting() && o.Build.LocalDockerHash == "" {
return fmt.Errorf("image for app '%s' is missing", o.App.Name)
}

// Expand env vars.
cloudRunHash := "unknown"

Expand Down
21 changes: 6 additions & 15 deletions deploy/dep_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@ type DatabaseDepOptionUser struct {
}

type DatabaseDepOptions struct {
Version string `json:"version"`
HA bool `json:"high_availability"`
Tier string `json:"tier" default:"db-f1-micro"`
Flags map[string]string `json:"flags"`
Users map[string]*DatabaseDepOptionUser `json:"users"`
DisableCloudSQLProxyUser bool `json:"disable_cloudsql_proxy_user"`
DatabaseVersion string `json:"-"`
Version string `json:"version"`
HA bool `json:"high_availability"`
Tier string `json:"tier" default:"db-f1-micro"`
Flags map[string]string `json:"flags"`
Users map[string]*DatabaseDepOptionUser `json:"users"`
DatabaseVersion string `json:"-"`
}

func NewDatabaseDepOptions(in map[string]interface{}, typ string) (*DatabaseDepOptions, error) {
Expand Down Expand Up @@ -198,14 +197,6 @@ func (o *DatabaseDep) Plan(pctx *config.PluginContext, r *registry.Registry, c *
}
}

if !o.Opts.DisableCloudSQLProxyUser {
if _, ok := users["cloudsqlproxy"]; !ok {
users["cloudsqlproxy"] = &DatabaseDepOptionUser{}
}

users["cloudsqlproxy"].Hostname = "cloudsqlproxy~%"
}

for u, p := range users {
err = o.registerUser(r, u, p.Password, p.Hostname)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions gcp/cloud_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type CloudSQL struct {
StartTime fields.StringInputField `default:"05:00"`
}

InsightsConfiguration struct {
Enabled fields.BoolInputField `default:"true"`
}

DatabaseFlags fields.MapInputField

SettingsVersion int64 `state:"-"`
Expand Down Expand Up @@ -83,6 +87,7 @@ func (o *CloudSQL) Read(ctx context.Context, meta interface{}) error {
o.IPConfiguration.Ipv4Enabled.SetCurrent(inst.Settings.IpConfiguration.Ipv4Enabled)
o.BackupConfiguration.Enabled.SetCurrent(inst.Settings.BackupConfiguration.Enabled)
o.BackupConfiguration.StartTime.SetCurrent(inst.Settings.BackupConfiguration.StartTime)
o.InsightsConfiguration.Enabled.SetCurrent(inst.Settings.InsightsConfig.QueryInsightsEnabled)

flags := make(map[string]interface{}, len(inst.Settings.DatabaseFlags))
for _, v := range inst.Settings.DatabaseFlags {
Expand Down Expand Up @@ -212,6 +217,9 @@ func (o *CloudSQL) makeDatabaseInstance() *sqladmin.DatabaseInstance {
StartTime: o.BackupConfiguration.StartTime.Wanted(),
Location: o.Region.Wanted()[:2],
},
InsightsConfig: &sqladmin.InsightsConfig{
QueryInsightsEnabled: o.InsightsConfiguration.Enabled.Wanted(),
},
DatabaseFlags: flags,
SettingsVersion: o.SettingsVersion,
},
Expand Down
28 changes: 15 additions & 13 deletions gcp/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ func (o *Image) push(ctx context.Context, meta interface{}) error {
return fmt.Errorf("error getting google credentials token: %w", err)
}

authConfig := dockertypes.AuthConfig{
Username: "oauth2accesstoken",
Password: token.AccessToken,
}

encodedJSON, err := json.Marshal(authConfig)
if err != nil {
return err
}

authStr := base64.URLEncoding.EncodeToString(encodedJSON)

cli, err := pctx.DockerClient()
if err != nil {
return err
Expand All @@ -159,7 +171,9 @@ func (o *Image) push(ctx context.Context, meta interface{}) error {

if o.Pull {
// Pull image from source.
reader, err := cli.ImagePull(ctx, o.Source.Wanted(), dockertypes.ImagePullOptions{})
reader, err := cli.ImagePull(ctx, o.Source.Wanted(), dockertypes.ImagePullOptions{
RegistryAuth: authStr,
})
if err != nil {
return err
}
Expand Down Expand Up @@ -190,18 +204,6 @@ func (o *Image) push(ctx context.Context, meta interface{}) error {
return err
}

authConfig := dockertypes.AuthConfig{
Username: "oauth2accesstoken",
Password: token.AccessToken,
}

encodedJSON, err := json.Marshal(authConfig)
if err != nil {
return err
}

authStr := base64.URLEncoding.EncodeToString(encodedJSON)

reader, err := cli.ImagePush(ctx, imageName, dockertypes.ImagePushOptions{
RegistryAuth: authStr,
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
github.com/google/go-containerregistry v0.8.0
github.com/mitchellh/mapstructure v1.4.3
github.com/outblocks/outblocks-plugin-go v0.0.0-20220408114357-5825c5c10810
github.com/outblocks/outblocks-plugin-go v0.0.0-20220411122407-4bcf87fcb89a
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b
google.golang.org/api v0.70.0
google.golang.org/grpc v1.45.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -769,8 +769,8 @@ github.com/opencontainers/selinux v1.8.0/go.mod h1:RScLhm78qiWa2gbVCcGkC7tCGdgk3
github.com/opencontainers/selinux v1.8.2/go.mod h1:MUIHuUEvKB1wtJjQdOyYRgOnLD2xAPP8dBsCoU0KuF8=
github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/outblocks/outblocks-plugin-go v0.0.0-20220408114357-5825c5c10810 h1:gjX5z6DrJo1FDulYEO7i2hGoSm0a0BjfxwyjAIdinH4=
github.com/outblocks/outblocks-plugin-go v0.0.0-20220408114357-5825c5c10810/go.mod h1:tcD3iwXc4UZ0H0ad9wvY41ZOqbG2UJ6bbl1Ha+/eyJc=
github.com/outblocks/outblocks-plugin-go v0.0.0-20220411122407-4bcf87fcb89a h1:O10irv/lK/pldUTlRgLsb78d19X3dwf5koJU29ncFpY=
github.com/outblocks/outblocks-plugin-go v0.0.0-20220411122407-4bcf87fcb89a/go.mod h1:tcD3iwXc4UZ0H0ad9wvY41ZOqbG2UJ6bbl1Ha+/eyJc=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand Down
4 changes: 4 additions & 0 deletions plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ commands:
short: "n"
type: string
usage: Dependency name to proxy
- name: user
short: "u"
type: string
usage: Database user to use
- name: port
short: "p"
type: integer
Expand Down
40 changes: 29 additions & 11 deletions plugin/command_dbproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,34 @@ func (p *Plugin) prepareTempFileCredentials() (f *os.File, err error) {
return f, err
}

func (p *Plugin) extractUser(ctx context.Context, registryData []byte, dep *apiv1.Dependency, user string) (*gcp.CloudSQLUser, error) {
if user == "" {
return nil, nil
}

reg := registry.NewRegistry(nil)

err := gcp.RegisterTypes(reg)
if err != nil {
return nil, err
}

err = reg.Load(ctx, registryData)
if err == nil {
u := &gcp.CloudSQLUser{}

if reg.GetDependencyResource(dep, user, u) {
return u, err
}
}

return nil, nil
}

func (p *Plugin) DBProxy(ctx context.Context, req *apiv1.CommandRequest) error {
flags := req.Args.Flags.AsMap()
name := flags["name"].(string)

user := flags["user"].(string)
port := int(flags["port"].(float64))

var defaultPort int
Expand Down Expand Up @@ -148,22 +172,16 @@ func (p *Plugin) DBProxy(ctx context.Context, req *apiv1.CommandRequest) error {
return err
}

var cloudsqluser *gcp.CloudSQLUser

err = reg.Load(ctx, req.PluginState.Registry)
if err == nil {
user := &gcp.CloudSQLUser{}

if reg.GetDependencyResource(dep.Dependency, "cloudsqlproxy", user) {
cloudsqluser = user
}
cloudsqluser, err := p.extractUser(ctx, req.PluginState.Registry, dep.Dependency, user)
if err != nil {
return err
}

if cloudsqluser != nil {
p.log.Infof("You can connect to it using user='%s', password='%s', host='127.0.0.1:%d'.\n",
cloudsqluser.Name.Any(), cloudsqluser.Password.Any(), port)
} else {
p.log.Infof("You can connect to it using credentials you defined and host='127.0.0.1:%d'.\n", port)
p.log.Infof("You can specify --user to use already created user or connect to it using credentials you defined and host='127.0.0.1:%d'.\n", port)
}

args := []string{"-instances", fmt.Sprintf("%s=tcp:%d", connectionName, port)}
Expand Down

0 comments on commit 23845d2

Please sign in to comment.