Add resourcealias application service
Introduce a standalone resourcealias package with its own service, IAM policies, and OAuth2 scopes so alias management no longer lives inside the trust center services. Remove the trust-center-specific alias services from probo and trust, and wire the new service into probod, the server, and the API layer. Signed-off-by: Bryan Frimin <bryan@probo.com>
This commit is contained in:
23
pkg/resourcealias/actions.go
Normal file
23
pkg/resourcealias/actions.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package resourcealias
|
||||
|
||||
// Resource alias service actions.
|
||||
// Format: resourcealias:alias:<action>
|
||||
const (
|
||||
ActionAliasGet = "resourcealias:alias:get"
|
||||
ActionAliasSet = "resourcealias:alias:set"
|
||||
ActionAliasRemove = "resourcealias:alias:remove"
|
||||
)
|
||||
33
pkg/resourcealias/oauth2_scopes.go
Normal file
33
pkg/resourcealias/oauth2_scopes.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package resourcealias
|
||||
|
||||
import "go.probo.inc/probo/pkg/coredata"
|
||||
|
||||
const (
|
||||
ScopeV1ResourceAliasRead coredata.OAuth2Scope = "v1:resource-alias:read"
|
||||
ScopeV1ResourceAlias coredata.OAuth2Scope = "v1:resource-alias"
|
||||
)
|
||||
|
||||
// OAuth2ScopeMappings maps OAuth2 scopes to resource-alias actions.
|
||||
var OAuth2ScopeMappings = map[coredata.OAuth2Scope][]string{
|
||||
ScopeV1ResourceAliasRead: {
|
||||
ActionAliasGet,
|
||||
},
|
||||
ScopeV1ResourceAlias: {
|
||||
ActionAliasSet,
|
||||
ActionAliasRemove,
|
||||
},
|
||||
}
|
||||
55
pkg/resourcealias/policies.go
Normal file
55
pkg/resourcealias/policies.go
Normal file
@@ -0,0 +1,55 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package resourcealias
|
||||
|
||||
import (
|
||||
"go.probo.inc/probo/pkg/iam"
|
||||
"go.probo.inc/probo/pkg/iam/policy"
|
||||
)
|
||||
|
||||
var organizationCondition = policy.Equals("principal.organization_id", "resource.organization_id")
|
||||
|
||||
// FullAccessPolicy grants complete resource-alias access to organization owners
|
||||
// and admins.
|
||||
var FullAccessPolicy = policy.NewPolicy(
|
||||
"resourcealias:full-access",
|
||||
"Resource Alias Full Access",
|
||||
policy.Allow(
|
||||
ActionAliasGet,
|
||||
ActionAliasSet,
|
||||
ActionAliasRemove,
|
||||
).WithSID("resource-alias-full-access").When(organizationCondition),
|
||||
).WithDescription("Full resource-alias access including set and remove")
|
||||
|
||||
// ReadAccessPolicy grants read-only resource-alias access to viewers and auditors.
|
||||
var ReadAccessPolicy = policy.NewPolicy(
|
||||
"resourcealias:read-access",
|
||||
"Resource Alias Read Access",
|
||||
policy.Allow(
|
||||
ActionAliasGet,
|
||||
).WithSID("resource-alias-read-access").When(organizationCondition),
|
||||
).WithDescription("Read-only resource-alias access")
|
||||
|
||||
// PolicySet returns the PolicySet for the resource-alias service. It is owned by
|
||||
// this package and registered into the authorizer at composition time so the
|
||||
// resource-alias authorization rules live alongside the resource-alias domain
|
||||
// logic instead of in the core probo policy set.
|
||||
func PolicySet() *iam.PolicySet {
|
||||
return iam.NewPolicySet().
|
||||
AddRolePolicy("OWNER", FullAccessPolicy).
|
||||
AddRolePolicy("ADMIN", FullAccessPolicy).
|
||||
AddRolePolicy("VIEWER", ReadAccessPolicy).
|
||||
AddRolePolicy("AUDITOR", ReadAccessPolicy)
|
||||
}
|
||||
185
pkg/resourcealias/service.go
Normal file
185
pkg/resourcealias/service.go
Normal file
@@ -0,0 +1,185 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package resourcealias
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/validator"
|
||||
)
|
||||
|
||||
const aliasMaxLength = 100
|
||||
|
||||
type (
|
||||
Service struct {
|
||||
pg *pg.Client
|
||||
}
|
||||
|
||||
CreateRequest struct {
|
||||
ResourceID gid.GID
|
||||
Alias string
|
||||
}
|
||||
)
|
||||
|
||||
func NewService(pgClient *pg.Client) *Service {
|
||||
return &Service{
|
||||
pg: pgClient,
|
||||
}
|
||||
}
|
||||
|
||||
func (req *CreateRequest) Validate() error {
|
||||
v := validator.New()
|
||||
|
||||
v.Check(req.ResourceID, "resource_id", validator.Required(), validator.GID())
|
||||
v.Check(req.Alias, "alias", validator.Required(), validator.Slug(aliasMaxLength))
|
||||
|
||||
return v.Error()
|
||||
}
|
||||
|
||||
func (s *Service) ResolveAlias(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
alias string,
|
||||
) (gid.GID, error) {
|
||||
record := &coredata.ResourceAlias{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
if err := record.LoadByAlias(ctx, conn, scope, alias); err != nil {
|
||||
return fmt.Errorf("cannot load resource alias: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return gid.Nil, err
|
||||
}
|
||||
|
||||
return record.ResourceID, nil
|
||||
}
|
||||
|
||||
func (s *Service) Create(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
req CreateRequest,
|
||||
) (*coredata.ResourceAlias, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
alias := &coredata.ResourceAlias{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
if err := alias.Upsert(ctx, conn, scope, req.ResourceID, req.Alias); err != nil {
|
||||
return fmt.Errorf("cannot create resource alias: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return alias, nil
|
||||
}
|
||||
|
||||
func (s *Service) Remove(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
resourceID gid.GID,
|
||||
) error {
|
||||
return s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
alias := &coredata.ResourceAlias{ResourceID: resourceID}
|
||||
if err := alias.Delete(ctx, conn, scope); err != nil {
|
||||
return fmt.Errorf("cannot remove resource alias: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Service) GetByResourceID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
resourceID gid.GID,
|
||||
) (*string, error) {
|
||||
record := &coredata.ResourceAlias{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
if err := record.LoadByResourceID(ctx, conn, scope, resourceID); err != nil {
|
||||
return fmt.Errorf("cannot load resource alias: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &record.Alias, nil
|
||||
}
|
||||
|
||||
func (s *Service) LoadByResourceIDs(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
resourceIDs []gid.GID,
|
||||
) (map[gid.GID]string, error) {
|
||||
if len(resourceIDs) == 0 {
|
||||
return map[gid.GID]string{}, nil
|
||||
}
|
||||
|
||||
var aliases coredata.ResourceAliases
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
if err := aliases.LoadByResourceIDs(ctx, conn, scope, resourceIDs); err != nil {
|
||||
return fmt.Errorf("cannot load resource aliases: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(map[gid.GID]string, len(aliases))
|
||||
for _, alias := range aliases {
|
||||
result[alias.ResourceID] = alias.Alias
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
Reference in New Issue
Block a user