github.com/Axway/agent-sdk@v1.1.101/pkg/apic/provisioning/credentialrequestdefinitionbuilder.go (about)

     1  package provisioning
     2  
     3  import (
     4  	"fmt"
     5  
     6  	management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
     7  	"github.com/Axway/agent-sdk/pkg/apic/definitions"
     8  	"github.com/Axway/agent-sdk/pkg/util"
     9  )
    10  
    11  // RegisterCredentialRequestDefinition - the function signature used when calling the NewCredentialRequestBuilder function
    12  type RegisterCredentialRequestDefinition func(credentialRequestDefinition *management.CredentialRequestDefinition) (*management.CredentialRequestDefinition, error)
    13  
    14  type credentialRequestDef struct {
    15  	name            string
    16  	title           string
    17  	provisionSchema map[string]interface{}
    18  	requestSchema   map[string]interface{}
    19  	webhooks        []string
    20  	actions         []string
    21  	registerFunc    RegisterCredentialRequestDefinition
    22  	err             error
    23  	agentDetails    map[string]interface{}
    24  	renewable       bool
    25  	suspendable     bool
    26  	period          int
    27  }
    28  
    29  // CredentialRequestBuilder - aids in creating a new credential request
    30  type CredentialRequestBuilder interface {
    31  	SetName(name string) CredentialRequestBuilder
    32  	SetTitle(title string) CredentialRequestBuilder
    33  	SetRequestSchema(schema SchemaBuilder) CredentialRequestBuilder
    34  	SetProvisionSchema(schema SchemaBuilder) CredentialRequestBuilder
    35  	SetWebhooks(webhooks []string) CredentialRequestBuilder
    36  	AddWebhook(webhook string) CredentialRequestBuilder
    37  	AddXAgentDetails(key string, value interface{}) CredentialRequestBuilder
    38  	IsRenewable() CredentialRequestBuilder
    39  	IsSuspendable() CredentialRequestBuilder
    40  	SetExpirationDays(days int) CredentialRequestBuilder
    41  	SetDeprovisionExpired() CredentialRequestBuilder
    42  	Register() (*management.CredentialRequestDefinition, error)
    43  }
    44  
    45  // NewCRDBuilder - called by the agent package and sends in the function that registers this credential request
    46  func NewCRDBuilder(registerFunc RegisterCredentialRequestDefinition) CredentialRequestBuilder {
    47  	return &credentialRequestDef{
    48  		webhooks:     make([]string, 0),
    49  		registerFunc: registerFunc,
    50  		actions:      make([]string, 0),
    51  		agentDetails: map[string]interface{}{},
    52  	}
    53  }
    54  
    55  // AddXAgentDetails - adds a key value pair to x-agent-details
    56  func (c *credentialRequestDef) AddXAgentDetails(key string, value interface{}) CredentialRequestBuilder {
    57  	c.agentDetails[key] = value
    58  	return c
    59  }
    60  
    61  // SetName - set the name of the credential request
    62  func (c *credentialRequestDef) SetName(name string) CredentialRequestBuilder {
    63  	c.name = util.NormalizeNameForCentral(name)
    64  	return c
    65  }
    66  
    67  // SetTitle - set the title of the credential request
    68  func (c *credentialRequestDef) SetTitle(title string) CredentialRequestBuilder {
    69  	c.title = title
    70  	return c
    71  }
    72  
    73  // SetRequestSchema - set the schema to be used for credential requests
    74  func (c *credentialRequestDef) SetRequestSchema(schema SchemaBuilder) CredentialRequestBuilder {
    75  	if c.err != nil {
    76  		return c
    77  	}
    78  
    79  	if schema != nil {
    80  		c.requestSchema, c.err = schema.Build()
    81  	} else {
    82  		c.err = fmt.Errorf("expected a SchemaBuilder argument but received nil")
    83  	}
    84  
    85  	return c
    86  }
    87  
    88  // SetProvisionSchema - set the schema to be used when provisioning credentials
    89  func (c *credentialRequestDef) SetProvisionSchema(schema SchemaBuilder) CredentialRequestBuilder {
    90  	if c.err != nil {
    91  		return c
    92  	}
    93  
    94  	if schema != nil {
    95  		c.provisionSchema, c.err = schema.Build()
    96  	} else {
    97  		c.err = fmt.Errorf("expected a SchemaBuilder argument but received nil")
    98  	}
    99  
   100  	return c
   101  }
   102  
   103  // SetWebhooks - set a list of webhooks to be invoked when credential of this type created
   104  func (c *credentialRequestDef) SetWebhooks(webhooks []string) CredentialRequestBuilder {
   105  	if webhooks != nil {
   106  		c.webhooks = webhooks
   107  	}
   108  	return c
   109  }
   110  
   111  // AddWebhook - add a webhook to the list of webhooks to be invoked when a credential of this type is requested
   112  func (c *credentialRequestDef) AddWebhook(webhook string) CredentialRequestBuilder {
   113  	c.webhooks = append(c.webhooks, webhook)
   114  	return c
   115  }
   116  
   117  // IsRenewable - the credential can be asked to be renewed
   118  func (c *credentialRequestDef) IsRenewable() CredentialRequestBuilder {
   119  	c.renewable = true
   120  	return c
   121  }
   122  
   123  // IsSuspendable - the credential can be asked to be suspended
   124  func (c *credentialRequestDef) IsSuspendable() CredentialRequestBuilder {
   125  	c.suspendable = true
   126  	return c
   127  }
   128  
   129  // SetExpirationDays - the number of days a credential of this type can live
   130  func (c *credentialRequestDef) SetExpirationDays(days int) CredentialRequestBuilder {
   131  	c.period = days
   132  	return c
   133  }
   134  
   135  // SetDeprovisionExpired - when set the agent will remove expired credentials from the data plane
   136  func (c *credentialRequestDef) SetDeprovisionExpired() CredentialRequestBuilder {
   137  	c.actions = append(c.actions, "deprovision")
   138  	return c
   139  }
   140  
   141  // Register - create the credential request definition and send it to Central
   142  func (c *credentialRequestDef) Register() (*management.CredentialRequestDefinition, error) {
   143  	if c.err != nil {
   144  		return nil, c.err
   145  	}
   146  
   147  	if c.requestSchema == nil {
   148  		c.requestSchema, _ = NewSchemaBuilder().Build()
   149  	}
   150  
   151  	spec := management.CredentialRequestDefinitionSpec{
   152  		Schema: c.requestSchema,
   153  		Provision: &management.CredentialRequestDefinitionSpecProvision{
   154  			Schema: c.provisionSchema,
   155  			Policies: management.CredentialRequestDefinitionSpecProvisionPolicies{
   156  				Renewable:   c.renewable,
   157  				Suspendable: c.suspendable,
   158  			},
   159  		},
   160  	}
   161  
   162  	if c.period > 0 {
   163  		spec.Provision.Policies.Expiry = &management.CredentialRequestDefinitionSpecProvisionPoliciesExpiry{
   164  			Period: int32(c.period),
   165  		}
   166  	}
   167  
   168  	hashInt, _ := util.ComputeHash(spec)
   169  
   170  	if c.title == "" {
   171  		c.title = c.name
   172  	}
   173  
   174  	crd := management.NewCredentialRequestDefinition(c.name, "")
   175  	crd.Title = c.title
   176  	crd.Spec = spec
   177  
   178  	util.SetAgentDetailsKey(crd, definitions.AttrSpecHash, fmt.Sprintf("%v", hashInt))
   179  
   180  	d := util.GetAgentDetails(crd)
   181  	for key, value := range c.agentDetails {
   182  		d[key] = value
   183  	}
   184  
   185  	util.SetAgentDetails(crd, d)
   186  
   187  	return c.registerFunc(crd)
   188  }