...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/msgdata/v1_request.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/msgdata

     1  package msgdata
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"edge-infra.dev/pkg/sds/emergencyaccess/apierror"
     9  	"edge-infra.dev/pkg/sds/emergencyaccess/eaconst"
    10  )
    11  
    12  type v1_0Request struct {
    13  	ReqAttr map[string]string `json:"attributes"`
    14  	ReqData v1Data            `json:"data"`
    15  	command string
    16  }
    17  
    18  type v1Data struct {
    19  	Command string `json:"command"`
    20  }
    21  
    22  // Create a structured v1.0 request from a full command string
    23  func NewV1_0Request(payload string) (Request, error) {
    24  	name, _, err := parsePayload(payload)
    25  	if err != nil {
    26  		return nil, apierror.E(apierror.ErrInvalidCommand, err)
    27  	}
    28  	req := v1_0Request{
    29  		ReqData: v1Data{
    30  			Command: payload,
    31  		},
    32  		ReqAttr: map[string]string{
    33  			eaconst.VersionKey:     string(eaconst.MessageVersion1_0),
    34  			eaconst.RequestTypeKey: string(eaconst.Command),
    35  		},
    36  		command: name,
    37  	}
    38  	if err := req.validate(); err != nil {
    39  		return nil, err
    40  	}
    41  	return req, nil
    42  }
    43  
    44  func (v v1_0Request) AddAttribute(key, val string) {
    45  	if _, ok := v.ReqAttr[key]; !ok {
    46  		v.ReqAttr[key] = val
    47  	}
    48  }
    49  
    50  func (v v1_0Request) Attributes() map[string]string {
    51  	return deepCopyMap(v.ReqAttr)
    52  }
    53  
    54  func (v v1_0Request) CommandToBeAuthorized() string {
    55  	return v.command
    56  }
    57  
    58  func (v v1_0Request) Data() ([]byte, error) {
    59  	return json.Marshal(v.ReqData)
    60  }
    61  
    62  func (v v1_0Request) RequestType() eaconst.RequestType {
    63  	return eaconst.Command
    64  }
    65  
    66  func (v v1_0Request) validate() error {
    67  	var err error
    68  	var apiErr error
    69  	if v.ReqData.Command == "" {
    70  		err = errors.Join(err, errors.New("command and args are empty"))
    71  	}
    72  	if v.command == "" {
    73  		apiErr = apierror.E(apierror.ErrInvalidCommand, "No command identified")
    74  		err = errors.Join(err, errors.New("command is empty"))
    75  	}
    76  	if attrErr := validateAttributes(v.ReqAttr, string(eaconst.MessageVersion1_0), string(eaconst.Command)); attrErr != nil {
    77  		err = errors.Join(err, attrErr)
    78  	}
    79  	if err != nil {
    80  		return apierror.E(apierror.ErrInvalidCommand, apiErr, err)
    81  	}
    82  	return nil
    83  }
    84  
    85  func assembleV1_0Request(data []byte, attributes map[string]string) (Request, error) {
    86  	var reqData v1Data
    87  	err := json.Unmarshal(data, &reqData)
    88  	if err != nil {
    89  		return nil, fmt.Errorf("failed to unmarshal: %w", err)
    90  	}
    91  	name, _, err := parsePayload(reqData.Command)
    92  	if err != nil {
    93  		return nil, apierror.E(apierror.ErrInvalidCommand, err)
    94  	}
    95  	req := v1_0Request{
    96  		ReqData: reqData,
    97  		ReqAttr: deepCopyMap(attributes),
    98  		command: name,
    99  	}
   100  	if err := req.validate(); err != nil {
   101  		return nil, err
   102  	}
   103  	return req, nil
   104  }
   105  

View as plain text