Skip to content

Commit

Permalink
Fix bug where sync failed if database name contained spaces. (#16)
Browse files Browse the repository at this point in the history
* Fix bug where sync failed if database name contained spaces.

* Fix docker compase command on ubuntu latest.

* Fix docker exec command.

* Upgrade baton-sdk to v0.2.13

* Add health check for sql server. Use new path to sqlcmd that microsoft broke without telling anyone. See microsoft/mssql-docker#892
  • Loading branch information
ggreer authored Aug 2, 2024
1 parent be843ec commit af10c3e
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 40 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
- 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: Install baton
run: ./scripts/get-baton.sh && mv baton /usr/local/bin
- name: Build baton-sql-server
run: go build ./cmd/baton-sql-server
- 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: Run baton-sql-server
run: ./baton-sql-server
- name: Check for grant before revoking
Expand Down
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ services:
- 1433:1433
volumes:
- db:/var/opt/mssql
healthcheck:
test: [ "CMD", "/opt/mssql-tools18/bin/sqlcmd","-C -U sa -P 'devP@ssw0rd' -Q 'SELECT * FROM INFORMATION_SCHEMA.TABLES'" ]
interval: 10s
timeout: 10s
retries: 6

volumes:
db:
Expand Down
2 changes: 1 addition & 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.8
github.com/conductorone/baton-sdk v0.2.13
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 Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,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.8 h1:DLHTQXhl50A5AB0cbSoMOmzrUwwbpP7poCXI69Lcdtw=
github.com/conductorone/baton-sdk v0.2.8/go.mod h1:cg5FyUcJnD7xK5SPbHe/KNpwUVVlpHJ9rnmd3UwxSkU=
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/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
8 changes: 4 additions & 4 deletions pkg/mssqldb/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ func (c *Client) ListDatabasePermissions(ctx context.Context, dbName string, pag
perms.state as state,
STRING_AGG(perms.type, ',') as perms,
principals.type as principal_type
FROM `)
FROM [`)
_, _ = sb.WriteString(dbName)
_, _ = sb.WriteString(`.sys.database_permissions perms
JOIN `)
_, _ = sb.WriteString(`].sys.database_permissions perms
JOIN [`)
_, _ = sb.WriteString(dbName)
_, _ = sb.WriteString(`.sys.database_principals AS principals
_, _ = sb.WriteString(`].sys.database_principals AS principals
ON perms.grantee_principal_id = principals.principal_id
WHERE (perms.state = 'G' OR perms.state = 'W') AND (perms.class = 0 AND perms.major_id = 0)
GROUP BY perms.grantee_principal_id, perms.state, principals.name, principals.type
Expand Down
18 changes: 9 additions & 9 deletions pkg/mssqldb/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ SELECT
sid,
name,
type_desc
FROM `)
FROM [`)
_, _ = sb.WriteString(dbName)
_, _ = sb.WriteString(`.sys.database_principals
_, _ = sb.WriteString(`].sys.database_principals
WHERE type = 'R'
ORDER BY
principal_id ASC OFFSET @p1 ROWS FETCH NEXT @p2 ROWS ONLY
Expand Down Expand Up @@ -212,14 +212,14 @@ func (c *Client) ListDatabaseRolePrincipals(ctx context.Context, dbName string,

query := fmt.Sprintf(
`SELECT
%s.sys.database_principals.principal_id,
%s.sys.database_principals.name,
%s.sys.database_principals.type
[%s].sys.database_principals.principal_id,
[%s].sys.database_principals.name,
[%s].sys.database_principals.type
FROM
%s.sys.database_principals
JOIN %s.sys.database_role_members ON %s.sys.database_role_members.member_principal_id = %s.sys.database_principals.principal_id
WHERE %s.sys.database_role_members.role_principal_id = @p1
ORDER BY %s.sys.database_principals.principal_id ASC OFFSET @p2 ROWS FETCH NEXT @p3 ROWS ONLY`,
[%s].sys.database_principals
JOIN [%s].sys.database_role_members ON [%s].sys.database_role_members.member_principal_id = [%s].sys.database_principals.principal_id
WHERE [%s].sys.database_role_members.role_principal_id = @p1
ORDER BY [%s].sys.database_principals.principal_id ASC OFFSET @p2 ROWS FETCH NEXT @p3 ROWS ONLY`,
dbName,
dbName,
dbName,
Expand Down
8 changes: 4 additions & 4 deletions pkg/mssqldb/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ SELECT
is_disabled
FROM
sys.server_principals
WHERE sid = (SELECT sid FROM `)
WHERE sid = (SELECT sid FROM [`)
_, _ = sb.WriteString(dbName)
_, _ = sb.WriteString(`.sys.database_principals WHERE principal_id = @p1)`)
_, _ = sb.WriteString(`].sys.database_principals WHERE principal_id = @p1)`)

row := c.db.QueryRowxContext(ctx, sb.String(), principalID)
if row.Err() != nil {
Expand Down Expand Up @@ -141,9 +141,9 @@ SELECT
principal_id,
name,
type_desc
FROM `)
FROM [`)
_, _ = sb.WriteString(dbName)
_, _ = sb.WriteString(`.sys.database_principals
_, _ = sb.WriteString(`].sys.database_principals
WHERE
(
type = 'S'
Expand Down
6 changes: 4 additions & 2 deletions vendor/github.com/conductorone/baton-sdk/pkg/config/config.go

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

6 changes: 3 additions & 3 deletions vendor/github.com/conductorone/baton-sdk/pkg/field/fields.go

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

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

31 changes: 30 additions & 1 deletion vendor/github.com/conductorone/baton-sdk/pkg/field/validation.go

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

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

3 changes: 1 addition & 2 deletions vendor/github.com/conductorone/baton-sdk/pkg/sdk/version.go

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

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

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

Loading

0 comments on commit af10c3e

Please sign in to comment.