Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for creating logins with known SID #141

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions internal/services/sqlLogin/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package sqlLogin
import (
"context"
"fmt"
"strconv"

"github.com/PGSSoft/terraform-provider-mssql/internal/core/resource"
common2 "github.com/PGSSoft/terraform-provider-mssql/internal/services/common"
"github.com/PGSSoft/terraform-provider-mssql/internal/validators"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"strconv"

"github.com/PGSSoft/terraform-provider-mssql/internal/sql"
"github.com/PGSSoft/terraform-provider-mssql/internal/utils"
Expand Down Expand Up @@ -40,6 +41,7 @@ func (d resourceData) toSettings(ctx context.Context) sql.SqlLoginSettings {
}

return sql.SqlLoginSettings{
Id: d.Id.ValueString(),
Name: d.Name.ValueString(),
Password: d.Password.ValueString(),
MustChangePassword: d.MustChangePassword.ValueBool(),
Expand Down Expand Up @@ -93,7 +95,7 @@ func (r *res) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource
resp.Schema.Attributes = map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: attrDescriptions["id"],
Computed: true,
Optional: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Expand Down
5 changes: 5 additions & 0 deletions internal/sql/sqlLogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
const NullLoginId LoginId = "<null>"

type SqlLoginSettings struct {
Id string
Name string
Password string
MustChangePassword bool
Expand All @@ -31,6 +32,10 @@ func (s SqlLoginSettings) toSqlOptions(ctx context.Context, conn Connection) str
builder := strings.Builder{}
builder.WriteString(fmt.Sprintf("PASSWORD='%s'", s.Password))

if s.Id != "" {
builder.WriteString(fmt.Sprintf(", SID=%s", s.Id))
}

if s.MustChangePassword && !isAzure {
builder.WriteString(" MUST_CHANGE")
}
Expand Down
5 changes: 5 additions & 0 deletions internal/sql/sqlLogin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ func (s *SqlLoginTestSuite) TestCreateSqlLogin() {
edition: EDITION_ENTERPRISE,
sql: "CREATE LOGIN [default_language] WITH PASSWORD='test_password', DEFAULT_LANGUAGE=[test_language], CHECK_EXPIRATION=OFF, CHECK_POLICY=ON",
},
"login with SID": {
settings: SqlLoginSettings{Name: "login_with_sid", Password: "test_password", Id: "0xE0EEDFAEC6DD7443BBFDE477C0D1E8A1"},
edition: EDITION_ENTERPRISE,
sql: "CREATE LOGIN [login_with_sid] WITH PASSWORD='test_password', SID=0xE0EEDFAEC6DD7443BBFDE477C0D1E8A1, CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF",
},
"Azure SQL": {
settings: SqlLoginSettings{
Name: "default_language",
Expand Down