这是indexloc提供的服务,不要输入任何密码
Skip to content

Parse teams with saml configs containing access group objects as role values #264

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

Merged
merged 4 commits into from
Jan 30, 2025
Merged
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
47 changes: 45 additions & 2 deletions client/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"encoding/json"
"fmt"

"github.com/hashicorp/terraform-plugin-log/tflog"
Expand All @@ -14,9 +15,51 @@ type TeamCreateRequest struct {
Plan string `json:"plan"`
}

type SamlRoleAccessGroupID struct {
AccessGroupID string `json:"accessGroupId"`
}

type SamlRole struct {
Role *string
AccessGroupID *SamlRoleAccessGroupID
}

func (f *SamlRole) UnmarshalJSON(data []byte) error {
var role string
if err := json.Unmarshal(data, &role); err == nil {
f.Role = &role
return nil
}
var ag SamlRoleAccessGroupID
if err := json.Unmarshal(data, &ag); err == nil {
f.AccessGroupID = &ag
return nil
}
return fmt.Errorf("received json is neither Role string nor AccessGroupID map")
}

type SamlRoles map[string]string

func (f *SamlRoles) UnmarshalJSON(data []byte) error {
var result map[string]SamlRole
if err := json.Unmarshal(data, &result); err != nil {
return err
}
tmp := make(SamlRoles)
for k, v := range result {
k := k
v := v
if v.Role != nil {
tmp[k] = *(v.Role)
}
}
*f = tmp
return nil
}

type SamlConfig struct {
Enforced bool `json:"enforced,omitempty"`
Roles map[string]string `json:"roles,omitempty"`
Enforced bool `json:"enforced,omitempty"`
Roles SamlRoles `json:"roles,omitempty"`
}

type TaxID struct {
Expand Down
35 changes: 35 additions & 0 deletions client/team_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package client

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)

func TestGetTeam(t *testing.T) {
type TestCase struct {
Name string
ResponseJSON string
}

for _, tc := range []TestCase{
{
Name: "SAML",
ResponseJSON: `{ "saml": { "roles": { "A": "OWNER", "B": { "accessGroupId": "foo" } } } }`,
},
} {
t.Run(tc.Name, func(t *testing.T) {
h := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
fmt.Fprintln(w, tc.ResponseJSON)
}))
cl := New("INVALID")
cl.baseURL = fmt.Sprintf("http://%s", h.Listener.Addr().String())
_, err := cl.GetTeam(context.Background(), "INVALID")
if err != nil {
t.Error(err)
}
})
}
}