Skip to content

Commit

Permalink
Add Realms realm (RO)
Browse files Browse the repository at this point in the history
  • Loading branch information
np5 committed Aug 16, 2024
1 parent 90497f7 commit 6ef0fd9
Show file tree
Hide file tree
Showing 3 changed files with 384 additions and 1 deletion.
6 changes: 5 additions & 1 deletion goztl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
libraryVersion = "0.1.50"
libraryVersion = "0.1.51"
userAgent = "goztl/" + libraryVersion
mediaType = "application/json"
)
Expand Down Expand Up @@ -72,6 +72,8 @@ type Client struct {
OsqueryFileCategories OsqueryFileCategoriesService
OsqueryPacks OsqueryPacksService
OsqueryQueries OsqueryQueriesService
// Realms
RealmsRealms RealmsRealmsService
// Santa
SantaConfigurations SantaConfigurationsService
SantaEnrollments SantaEnrollmentsService
Expand Down Expand Up @@ -198,6 +200,8 @@ func NewClient(httpClient *http.Client, bu string, token string, opts ...ClientO
c.OsqueryFileCategories = &OsqueryFileCategoriesServiceOp{client: c}
c.OsqueryPacks = &OsqueryPacksServiceOp{client: c}
c.OsqueryQueries = &OsqueryQueriesServiceOp{client: c}
// Realms
c.RealmsRealms = &RealmsRealmsServiceOp{client: c}
// Santa
c.SantaConfigurations = &SantaConfigurationsServiceOp{client: c}
c.SantaEnrollments = &SantaEnrollmentsServiceOp{client: c}
Expand Down
150 changes: 150 additions & 0 deletions realms_realms.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package goztl

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

const rBasePath = "realms/realms/"

// RealmsRealmsService is an interface for interfacing with the realms
// endpoints of the Zentral API
type RealmsRealmsService interface {
List(context.Context, *ListOptions) ([]RealmsRealm, *Response, error)
GetByUUID(context.Context, string) (*RealmsRealm, *Response, error)
GetByName(context.Context, string) (*RealmsRealm, *Response, error)
}

// RealmsRealmsServiceOp handles communication with the realms related
// methods of the Zentral API.
type RealmsRealmsServiceOp struct {
client *Client
}

var _ RealmsRealmsService = &RealmsRealmsServiceOp{}

// LDAPConfig represents a Zentral Realm LDAP config
type LDAPConfig struct {
Host string `json:"host"`
BindDN string `json:"bind_dn"`
BindPassword string `json:"bind_password"`
UsersBaseDN string `json:"users_base_dn"`
}

// OpenIDCConfig represents a Zentral Realm OpenIDC config
type OpenIDCConfig struct {
DiscoveryURL string `json:"discovery_url"`
ClientID string `json:"client_id"`
ClientSecret *string `json:"client_secret"`
ExtraScopes []string `json:"extra_scopes"`
}

// SAMLConfig represents a Zentral Realm SAML config
type SAMLConfig struct {
DefaultRelayState string `json:"default_relay_state"`
IDPMetadata string `json:"idp_metadata"`
}

// RealmsRealm represents a Zentral realm
type RealmsRealm struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Backend string `json:"backend"`
LDAPConfig *LDAPConfig `json:"ldap_config"`
OpenIDCConfig *OpenIDCConfig `json:"openidc_config"`
SAMLConfig *SAMLConfig `json:"saml_config"`
EnabledForLogin bool `json:"enabled_for_login"`
LoginSessionExpiry int `json:"login_session_expiry"`
UsernameClaim string `json:"username_claim"`
EmailClaim string `json:"email_claim"`
FirstNameClaim string `json:"first_name_claim"`
LastNameClaim string `json:"last_name_claim"`
FullNameClaim string `json:"full_name_claim"`
CustomAttr1Claim string `json:"custom_attr_1_claim"`
CustomAttr2Claim string `json:"custom_attr_2_claim"`
SCIMEnabled bool `json:"scim_enabled"`
Created Timestamp `json:"created_at"`
Updated Timestamp `json:"updated_at"`
}

func (r RealmsRealm) String() string {
return Stringify(r)
}

type listROptions struct {
Name string `url:"name,omitempty"`
}

// List lists all the Realms realms.
func (s *RealmsRealmsServiceOp) List(ctx context.Context, opt *ListOptions) ([]RealmsRealm, *Response, error) {
return s.list(ctx, opt, nil)
}

// GetByID retrieves a Realms realm by id.
func (s *RealmsRealmsServiceOp) GetByUUID(ctx context.Context, rUUID string) (*RealmsRealm, *Response, error) {
if len(rUUID) < 1 {
return nil, nil, NewArgError("rUUID", "cannot be empty")
}

path := fmt.Sprintf("%s%s/", rBasePath, rUUID)

req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

r := new(RealmsRealm)

resp, err := s.client.Do(ctx, req, r)
if err != nil {
return nil, resp, err
}

return r, resp, err
}

// GetByName retrieves a Realms realm by name.
func (s *RealmsRealmsServiceOp) GetByName(ctx context.Context, name string) (*RealmsRealm, *Response, error) {
if len(name) < 1 {
return nil, nil, NewArgError("name", "cannot be blank")
}

listROpt := &listROptions{Name: name}

rs, resp, err := s.list(ctx, nil, listROpt)
if err != nil {
return nil, resp, err
}
if len(rs) < 1 {
return nil, resp, nil
}

return &rs[0], resp, err
}

// Helper method for listing Realms realms
func (s *RealmsRealmsServiceOp) list(ctx context.Context, opt *ListOptions, rOpt *listROptions) ([]RealmsRealm, *Response, error) {
path := rBasePath
path, err := addOptions(path, opt)
if err != nil {
return nil, nil, err
}
path, err = addOptions(path, rOpt)
if err != nil {
return nil, nil, err
}

req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

var rs []RealmsRealm
resp, err := s.client.Do(ctx, req, &rs)
if err != nil {
return nil, resp, err
}

return rs, resp, err
}
Loading

0 comments on commit 6ef0fd9

Please sign in to comment.