Skip to content

Commit

Permalink
Add option to skip unavailable databases.
Browse files Browse the repository at this point in the history
  • Loading branch information
ggreer committed Oct 4, 2024
1 parent e6ab134 commit 7acf204
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 32 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Flags:
--log-format string The output format for logs: json, console ($BATON_LOG_FORMAT) (default "json")
--log-level string The log level: debug, info, warn, error ($BATON_LOG_LEVEL) (default "info")
-p, --provisioning This must be set in order for provisioning actions to be enabled ($BATON_PROVISIONING)
--skip-unavailable-databases Skip databases that are unavailable (offline, restoring, etc) ($BATON_SKIP_UNAVAILABLE_DATABASES)
--ticketing This must be set to enable ticketing support ($BATON_TICKETING)
-v, --version version for baton-sql-server
Expand Down
17 changes: 3 additions & 14 deletions cmd/baton-sql-server/config.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
package main

import (
"context"
"fmt"

"github.com/conductorone/baton-sdk/pkg/field"
"github.com/spf13/viper"
)

var (
dsn = field.StringField("dsn",
field.WithDescription("The connection string for connecting to SQL Server"),
field.WithRequired(true))
skipUnavailableDatabases = field.BoolField("skip-unavailable-databases",
field.WithDescription("Skip databases that are unavailable (offline, restoring, etc)"))
)

var cfg = field.Configuration{
Fields: []field.SchemaField{dsn},
}

// validateConfig is run after the configuration is loaded, and should return an error if it isn't valid.
func validateConfig(_ context.Context, v *viper.Viper) error {
if v.GetString(dsn.FieldName) == "" {
return fmt.Errorf("--dsn is required")
}

return nil
Fields: []field.SchemaField{dsn, skipUnavailableDatabases},
}
6 changes: 1 addition & 5 deletions cmd/baton-sql-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ func main() {
func getConnector(ctx context.Context, v *viper.Viper) (types.ConnectorServer, error) {
l := ctxzap.Extract(ctx)

if err := validateConfig(ctx, v); err != nil {
return nil, err
}

cb, err := connector.New(ctx, v.GetString(dsn.FieldName))
cb, err := connector.New(ctx, v.GetString(dsn.FieldName), v.GetBool(skipUnavailableDatabases.FieldName))
if err != nil {
l.Error("error creating connector", zap.Error(err))
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (o *Mssqldb) ResourceSyncers(ctx context.Context) []connectorbuilder.Resour
}
}

func New(ctx context.Context, dsn string) (*Mssqldb, error) {
c, err := mssqldb.New(ctx, dsn)
func New(ctx context.Context, dsn string, skipUnavailableDatabases bool) (*Mssqldb, error) {
c, err := mssqldb.New(ctx, dsn, skipUnavailableDatabases)
if err != nil {
return nil, err
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/connector/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestClientListDatabasePermissions(t *testing.T) {
t.Skip()
}

cli, err := mssqldb.New(ctx, dsn)
cli, err := mssqldb.New(ctx, dsn, false)
assert.Nil(t, err)

for _, test := range tests {
Expand All @@ -69,7 +69,7 @@ func TestClientListServerPermissions(t *testing.T) {
t.Skip()
}

cli, err := mssqldb.New(ctx, dsn)
cli, err := mssqldb.New(ctx, dsn, false)
assert.Nil(t, err)

for keepingPagination(pager.Token) {
Expand All @@ -84,7 +84,7 @@ func TestClientListServerRoles(t *testing.T) {
t.Skip()
}

cli, err := mssqldb.New(ctx, dsn)
cli, err := mssqldb.New(ctx, dsn, false)
assert.Nil(t, err)

for keepingPagination(pager.Token) {
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestClientListDatabaseRoles(t *testing.T) {
t.Skip()
}

cli, err := mssqldb.New(ctx, dsn)
cli, err := mssqldb.New(ctx, dsn, false)
assert.Nil(t, err)

for _, test := range tests {
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestClientListDatabaseRolePrincipals(t *testing.T) {
t.Skip()
}

cli, err := mssqldb.New(ctx, dsn)
cli, err := mssqldb.New(ctx, dsn, false)
assert.Nil(t, err)

for _, test := range tests {
Expand All @@ -197,7 +197,7 @@ func TestClientListServerRolePrincipals(t *testing.T) {
t.Skip()
}

cli, err := mssqldb.New(ctx, dsn)
cli, err := mssqldb.New(ctx, dsn, false)
assert.Nil(t, err)

for keepingPagination(pager.Token) {
Expand All @@ -212,7 +212,7 @@ func TestClientListDatabases(t *testing.T) {
t.Skip()
}

cli, err := mssqldb.New(ctx, dsn)
cli, err := mssqldb.New(ctx, dsn, false)
assert.Nil(t, err)

for keepingPagination(pager.Token) {
Expand Down
8 changes: 5 additions & 3 deletions pkg/mssqldb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

type Client struct {
db *sqlx.DB
db *sqlx.DB
skipUnavailableDatabases bool
}

// List databases
Expand All @@ -20,7 +21,7 @@ type Client struct {

// List users

func New(ctx context.Context, dsn string) (*Client, error) {
func New(ctx context.Context, dsn string, skipUnavailableDatabases bool) (*Client, error) {
db, err := sqlx.Connect("sqlserver", dsn)
if err != nil {
return nil, err
Expand All @@ -31,7 +32,8 @@ func New(ctx context.Context, dsn string) (*Client, error) {
db.SetMaxIdleConns(1)

c := &Client{
db: db,
db: db,
skipUnavailableDatabases: skipUnavailableDatabases,
}

return c, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/mssqldb/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestClient_GetServerPrincipalForDatabaseUser(t *testing.T) {
t.Skip("requires a running SQL Server instance")
ctx := context.Background()

c, err := New(ctx, "server=127.0.0.1;user id=sa;password=devP@ssw0rd;port=1433")
c, err := New(ctx, "server=127.0.0.1;user id=sa;password=devP@ssw0rd;port=1433", false)
require.NoError(t, err)

u, err := c.GetServerPrincipalForDatabasePrincipal(ctx, "master", 7)
Expand Down

0 comments on commit 7acf204

Please sign in to comment.