Skip to content

Commit

Permalink
Merge pull request #43 from permitio/raz/per-10081-golang-sdk-local-f…
Browse files Browse the repository at this point in the history
…acts-uploader

add facts api to go sdk
  • Loading branch information
RazcoDev authored Jun 27, 2024
2 parents edb6071 + 52f9017 commit f6eab5d
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 52 deletions.
78 changes: 70 additions & 8 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package api

import (
"context"
"fmt"
"github.com/permitio/permit-golang/pkg/config"
"github.com/permitio/permit-golang/pkg/errors"
"github.com/permitio/permit-golang/pkg/openapi"
"go.uber.org/zap"
"time"
)

type permitBaseApi struct {
Expand All @@ -14,10 +16,37 @@ type permitBaseApi struct {
logger *zap.Logger
}

type PermitBaseFactsApi struct {
permitBaseApi
}

type IPermitBaseFactsApi interface {
lazyLoadPermitContext(ctx context.Context, methodApiLevelArg ...config.APIKeyLevel) error
WaitForSync(timeout *time.Duration) *PermitBaseFactsApi
}

type IPermitBaseApi interface {
lazyLoadPermitContext(ctx context.Context, methodApiLevelArg ...config.APIKeyLevel) error
}

func (a *PermitBaseFactsApi) WaitForSync(timeout *time.Duration) *PermitBaseFactsApi {
if a.config.GetProxyFactsViaPDP() {
stringTimeout := ""
if timeout == nil {
if timeoutFromConfig := a.config.GetFactsSyncTimeout(); timeoutFromConfig != nil {
stringTimeout = fmt.Sprintf("%d", int64(timeoutFromConfig.Seconds()))
}
}

clientConfig := a.client.GetConfig()
clientConfig.DefaultHeader["X-Wait-Timeout"] = stringTimeout
return NewPermitBaseFactsApi(openapi.NewAPIClient(clientConfig), a.config)
} else {
a.logger.Warn("Attempted to wait for sync, but 'proxyFactsViaPdp' is not enabled. Ignoring")
return a
}
}

func (a *permitBaseApi) lazyLoadPermitContext(ctx context.Context, methodApiLevelArg ...config.APIKeyLevel) error {
var methodApiLevel config.APIKeyLevel
permitContext := a.config.Context.GetContext()
Expand Down Expand Up @@ -84,14 +113,37 @@ func (p *PermitApiClient) SetContext(ctx context.Context, project string, enviro
}
p.config.Context = permitContext
}

func NewPermitApiClient(ctx context.Context, config *config.PermitConfig) *PermitApiClient {
func NewClientConfig(config *config.PermitConfig) *openapi.Configuration {
clientConfig := openapi.NewConfiguration()
clientConfig.Host = getHostFromUrl(config.GetApiUrl())
clientConfig.Scheme = getSchemaFromUrl(config.GetApiUrl())
clientConfig.AddDefaultHeader("Authorization", "Bearer "+config.GetToken())
clientConfig.HTTPClient = config.GetHTTPClient()
client := openapi.NewAPIClient(clientConfig)
return clientConfig
}
func NewFactsClientConfig(config *config.PermitConfig) *openapi.Configuration {
clientConfig := openapi.NewConfiguration()
stringTimeout := ""

//if timeout == nil {
if timeoutFromConfig := config.GetFactsSyncTimeout(); timeoutFromConfig != nil {
stringTimeout = fmt.Sprintf("%d", int64(timeoutFromConfig.Seconds()))
}
//}
clientConfig.DefaultHeader["X-Wait-Timeout"] = stringTimeout

clientConfig.AddDefaultHeader("Authorization", "Bearer "+config.GetToken())
clientConfig.Host = getHostFromUrl(config.GetPdpUrl())
clientConfig.Scheme = getSchemaFromUrl(config.GetPdpUrl())
clientConfig.HTTPClient = config.GetHTTPClient()
return clientConfig
}

func NewPermitApiClient(config *config.PermitConfig) *PermitApiClient {
baseClientConfig := NewClientConfig(config)
factsClientConfig := NewFactsClientConfig(config)
client := openapi.NewAPIClient(baseClientConfig)
factsClient := openapi.NewAPIClient(factsClientConfig)
return &PermitApiClient{
config: config,
logger: config.Logger,
Expand All @@ -102,17 +154,27 @@ func NewPermitApiClient(ctx context.Context, config *config.PermitConfig) *Permi
ImplicitGrants: NewImplicitGrantsApi(client, config),
Projects: NewProjectsApi(client, config),
ProxyConfigs: NewProxyConfigsApi(client, config),
RelationshipTuples: NewRelationshipTuplesApi(client, config),
RelationshipTuples: NewRelationshipTuplesApi(factsClient, config),
ResourceActionGroups: NewResourceActionGroupsApi(client, config),
ResourceActions: NewResourceActionsApi(client, config),
ResourceAttributes: NewResourceAttributesApi(client, config),
ResourceInstances: NewResourceInstancesApi(client, config),
ResourceInstances: NewResourceInstancesApi(factsClient, config),
ResourceRelations: NewResourceRelationsApi(client, config),
ResourceRoles: NewResourceRolesApi(client, config),
Resources: NewResourcesApi(client, config),
RoleAssignments: NewRoleAssignmentsApi(client, config),
RoleAssignments: NewRoleAssignmentsApi(factsClient, config),
Roles: NewRolesApi(client, config),
Tenants: NewTenantsApi(client, config),
Users: NewUsersApi(client, config),
Tenants: NewTenantsApi(factsClient, config),
Users: NewUsersApi(factsClient, config),
}
}

func NewPermitBaseFactsApi(client *openapi.APIClient, config *config.PermitConfig) *PermitBaseFactsApi {
return &PermitBaseFactsApi{
permitBaseApi: permitBaseApi{
client: client,
config: config,
logger: config.Logger,
},
}
}
16 changes: 11 additions & 5 deletions pkg/api/relationshipTuples.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@ import (
"github.com/permitio/permit-golang/pkg/models"
"github.com/permitio/permit-golang/pkg/openapi"
"go.uber.org/zap"
"time"
)

type RelationshipTuples struct {
permitBaseApi
PermitBaseFactsApi
}

func NewRelationshipTuplesApi(client *openapi.APIClient, config *config.PermitConfig) *RelationshipTuples {
return &RelationshipTuples{
permitBaseApi{
client: client,
config: config,
logger: config.Logger,
PermitBaseFactsApi{
permitBaseApi{
client: client,
config: config,
logger: config.Logger,
},
},
}
}
func (u *RelationshipTuples) WaitForSync(timeout *time.Duration) *RelationshipTuples {
return NewRelationshipTuplesApi(u.PermitBaseFactsApi.WaitForSync(timeout).client, u.config)
}

func (r *RelationshipTuples) Create(
ctx context.Context,
Expand Down
17 changes: 12 additions & 5 deletions pkg/api/resourceInstances.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@ import (
"github.com/permitio/permit-golang/pkg/models"
"github.com/permitio/permit-golang/pkg/openapi"
"go.uber.org/zap"
"time"
)

type ResourceInstances struct {
permitBaseApi
PermitBaseFactsApi
}

func NewResourceInstancesApi(client *openapi.APIClient, config *config.PermitConfig) *ResourceInstances {
return &ResourceInstances{
permitBaseApi{
client: client,
config: config,
logger: config.Logger,
PermitBaseFactsApi{
permitBaseApi{
client: client,
config: config,
logger: config.Logger,
},
},
}
}

func (r *ResourceInstances) WaitForSync(timeout *time.Duration) *ResourceInstances {
return NewResourceInstancesApi(r.PermitBaseFactsApi.WaitForSync(timeout).client, r.config)
}

func (r *ResourceInstances) Create(
ctx context.Context,
resourceInstanceCreate models.ResourceInstanceCreate,
Expand Down
17 changes: 12 additions & 5 deletions pkg/api/roleAssignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@ import (
"github.com/permitio/permit-golang/pkg/models"
"github.com/permitio/permit-golang/pkg/openapi"
"go.uber.org/zap"
"time"
)

type RoleAssignments struct {
permitBaseApi
PermitBaseFactsApi
}

func NewRoleAssignmentsApi(client *openapi.APIClient, config *config.PermitConfig) *RoleAssignments {
return &RoleAssignments{
permitBaseApi{
client: client,
config: config,
logger: config.Logger,
PermitBaseFactsApi{
permitBaseApi{
client: client,
config: config,
logger: config.Logger,
},
},
}
}

func (r *RoleAssignments) WaitForSync(timeout *time.Duration) *RoleAssignments {
return NewRoleAssignmentsApi(r.PermitBaseFactsApi.WaitForSync(timeout).client, r.config)
}

func (r *RoleAssignments) List(ctx context.Context, page int, perPage int, userFilter, roleFilter, tenantFilter string) (*[]models.RoleAssignmentRead, error) {
response, err := r.list(ctx, page, perPage, userFilter, roleFilter, tenantFilter, false)
if err != nil {
Expand Down
17 changes: 12 additions & 5 deletions pkg/api/tenants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@ import (
"github.com/permitio/permit-golang/pkg/models"
"github.com/permitio/permit-golang/pkg/openapi"
"go.uber.org/zap"
"time"
)

type Tenants struct {
permitBaseApi
PermitBaseFactsApi
}

func NewTenantsApi(client *openapi.APIClient, config *config.PermitConfig) *Tenants {
return &Tenants{
permitBaseApi{
client: client,
config: config,
logger: config.Logger,
PermitBaseFactsApi{
permitBaseApi{
client: client,
config: config,
logger: config.Logger,
},
},
}
}

func (t *Tenants) WaitForSync(timeout *time.Duration) *Tenants {
return NewTenantsApi(t.PermitBaseFactsApi.WaitForSync(timeout).client, t.config)
}

// List all tenants under the context's environment.
// Usage Example:
// `tenants, err := PermitClient.Api.Tenants.List(ctx, 1, 10)`
Expand Down
17 changes: 12 additions & 5 deletions pkg/api/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,29 @@ import (
"github.com/permitio/permit-golang/pkg/openapi"
"go.uber.org/zap"
"strings"
"time"
)

type Users struct {
permitBaseApi
PermitBaseFactsApi
}

func NewUsersApi(client *openapi.APIClient, config *config.PermitConfig) *Users {
return &Users{
permitBaseApi{
client: client,
config: config,
logger: config.Logger,
PermitBaseFactsApi{
permitBaseApi: permitBaseApi{
client: client,
config: config,
logger: config.Logger,
},
},
}
}

func (u *Users) WaitForSync(timeout *time.Duration) *Users {
return NewUsersApi(u.PermitBaseFactsApi.WaitForSync(timeout).client, u.config)
}

// List the users from your context's environment.
// Usage Example:
//
Expand Down
26 changes: 19 additions & 7 deletions pkg/config/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,31 @@ type PermitBuilder struct {
}

func NewConfigBuilder(token string) *PermitBuilder {
factsSyncTimeout := DefaultFactsSyncTimeout
return &PermitBuilder{
PermitConfig: PermitConfig{
apiUrl: DefaultApiUrl,
token: token,
pdpUrl: DefaultPdpUrl,
debug: DefaultDebugMode,
Context: nil,
Logger: nil,
httpClient: &http.Client{Timeout: DefaultTimeout},
apiUrl: DefaultApiUrl,
token: token,
pdpUrl: DefaultPdpUrl,
debug: DefaultDebugMode,
Context: nil,
Logger: nil,
proxyFactsViaPDP: false,
factsSyncTimeout: &factsSyncTimeout,
httpClient: &http.Client{Timeout: DefaultTimeout},
},
}
}

func (c *PermitConfig) WithProxyFactsViaPDP(proxyFactsViaPDP bool) *PermitConfig {
c.proxyFactsViaPDP = proxyFactsViaPDP
return c
}

func (c *PermitConfig) WithFactsSyncTimeout(timeout time.Duration) *PermitConfig {
c.factsSyncTimeout = &timeout
return c
}
func (c *PermitConfig) WithHTTPClient(client *http.Client) *PermitConfig {
c.httpClient = client
return c
Expand Down
30 changes: 22 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package config
import (
"go.uber.org/zap"
"net/http"
"time"
)

type PermitConfig struct {
apiUrl string
token string
pdpUrl string
opaUrl string
debug bool
Context *PermitContext
Logger *zap.Logger
httpClient *http.Client
apiUrl string
token string
pdpUrl string
opaUrl string
debug bool
Context *PermitContext
Logger *zap.Logger
httpClient *http.Client
proxyFactsViaPDP bool
factsSyncTimeout *time.Duration
}

type IPermitConfig interface {
Expand All @@ -24,6 +27,9 @@ type IPermitConfig interface {
GetDebug() bool
GetContext() *PermitContext
GetLogger() *zap.Logger
GetProxyFactsViaPDP() bool
GetFactsSyncTimeout() *int64
GetHTTPClient() *http.Client
}

func NewPermitConfig(apiUrl string, token string, pdpUrl string, debug bool, context *PermitContext, logger *zap.Logger) *PermitConfig {
Expand Down Expand Up @@ -65,6 +71,14 @@ func (c *PermitConfig) GetLogger() *zap.Logger {
return c.Logger
}

func (c *PermitConfig) GetProxyFactsViaPDP() bool {
return c.proxyFactsViaPDP
}

func (c *PermitConfig) GetFactsSyncTimeout() *time.Duration {
return c.factsSyncTimeout
}

func (c *PermitConfig) GetHTTPClient() *http.Client {
return c.httpClient
}
3 changes: 2 additions & 1 deletion pkg/config/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ const (
DefaultPdpUrl = "http://localhost:7766"
DefaultOpaUrl = "http://localhost:8181"

DefaultTimeout = time.Second * 5
DefaultTimeout = time.Second * 5
DefaultFactsSyncTimeout = 10 * time.Second
)
Loading

0 comments on commit f6eab5d

Please sign in to comment.