Skip to content

Commit

Permalink
Add option to skip unavailable databases. (#17)
Browse files Browse the repository at this point in the history
* Upgrade baton-sdk to v0.2.35.

* Generate capabilities.

* Add option to skip unavailable databases.
  • Loading branch information
ggreer authored Oct 5, 2024
1 parent af10c3e commit 981738f
Show file tree
Hide file tree
Showing 87 changed files with 6,508 additions and 1,229 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/capabilities.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ jobs:
token: ${{ secrets.RELENG_GITHUB_TOKEN }}

- name: Run Docker Compose as a Daemon (to start sql server)
run: docker-compose -f ./docker-compose.yml up --detach
run: docker compose -f ./docker-compose.yml up --detach
- name: Create another database
run: docker exec -t baton-sql-server-db-1 /opt/mssql-tools18/bin/sqlcmd -C -S localhost -U SA -P 'devP@ssw0rd' -Q 'create database [space test 1]'

- name: Setup Go
uses: actions/setup-go@v5
Expand All @@ -29,6 +31,8 @@ jobs:
run: go build -o connector ./cmd/baton-sql-server

- name: Run and save output
env:
BATON_DSN: server=127.0.0.1;user id=sa;password=devP@ssw0rd;port=1433
run: ./connector capabilities > baton_capabilities.json

- name: Commit changes
Expand Down
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/conductorone/baton-sql-server
go 1.21

require (
github.com/conductorone/baton-sdk v0.2.13
github.com/conductorone/baton-sdk v0.2.35
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/jmoiron/sqlx v1.3.5
github.com/microsoft/go-mssqldb v1.3.0
Expand All @@ -15,6 +15,7 @@ require (
require (
filippo.io/age v1.1.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/allegro/bigcache/v3 v3.1.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.26.1 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 // indirect
github.com/aws/aws-sdk-go-v2/config v1.27.11 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0/go.mod h1:kgDm
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk=
github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
github.com/aws/aws-sdk-go-v2 v1.26.1 h1:5554eUqIYVWpU0YmeeYZ0wU64H2VLBs8TlhRB2L+EkA=
github.com/aws/aws-sdk-go-v2 v1.26.1/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 h1:x6xsQXGSmW6frevwDA+vi/wqhp1ct18mVXYN08/93to=
Expand Down Expand Up @@ -54,8 +56,8 @@ github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZx
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/conductorone/baton-sdk v0.2.13 h1:/9wJGlbiPxDQmjqKOTqyKpJ1Ui2+o7oYHInMqqR1PyQ=
github.com/conductorone/baton-sdk v0.2.13/go.mod h1:cg5FyUcJnD7xK5SPbHe/KNpwUVVlpHJ9rnmd3UwxSkU=
github.com/conductorone/baton-sdk v0.2.35 h1:aSdNvlM5HMti8WdhotrXTHWs+b+BmSqMxtGwsSUFxjY=
github.com/conductorone/baton-sdk v0.2.35/go.mod h1:hmd/Oz3DPIKD+9QmkusZaA18ZoiinnTDdrxh2skcdUc=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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
11 changes: 8 additions & 3 deletions pkg/mssqldb/databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
const DatabaseType = "database"

type DbModel struct {
ID int64 `db:"database_id"`
Name string `db:"name"`
ID int64 `db:"database_id"`
Name string `db:"name"`
StateDesc string `db:"state_desc"`
}

func (c *Client) GetDatabase(ctx context.Context, id int64) (*DbModel, error) {
Expand Down Expand Up @@ -48,7 +49,7 @@ func (c *Client) ListDatabases(ctx context.Context, pager *Pager) ([]*DbModel, s
args := []interface{}{offset, limit + 1}

var sb strings.Builder
_, _ = sb.WriteString(`SELECT name, database_id FROM sys.databases
_, _ = sb.WriteString(`SELECT name, database_id, state_desc FROM sys.databases
ORDER BY database_id ASC
OFFSET @p1 ROWS
FETCH NEXT @p2 ROWS ONLY`)
Expand All @@ -68,6 +69,10 @@ func (c *Client) ListDatabases(ctx context.Context, pager *Pager) ([]*DbModel, s
if err != nil {
return nil, "", err
}
if c.skipUnavailableDatabases && dbModel.StateDesc != "ONLINE" {
l.Info("Skipping sync of unavailable database", zap.String("name", dbModel.Name), zap.String("state", dbModel.StateDesc))
continue
}
ret = append(ret, &dbModel)
}
if rows.Err() != 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
28 changes: 28 additions & 0 deletions vendor/github.com/allegro/bigcache/v3/.codecov.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions vendor/github.com/allegro/bigcache/v3/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 981738f

Please sign in to comment.