emissary/pkg/client/register_agent.go

49 lines
1.2 KiB
Go

package client
import (
"context"
"forge.cadoles.com/Cadoles/emissary/internal/agent/metadata"
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
"forge.cadoles.com/Cadoles/emissary/internal/jwk"
"github.com/pkg/errors"
)
func (c *Client) RegisterAgent(ctx context.Context, key Key, thumbprint string, meta []MetadataTuple, funcs ...OptionFunc) (*Agent, error) {
keySet, err := jwk.RS256PublicKeySet(key)
if err != nil {
return nil, errors.WithStack(err)
}
signature, err := jwk.Sign(key, thumbprint, meta)
if err != nil {
return nil, errors.WithStack(err)
}
payload := struct {
KeySet jwk.Set `json:"keySet"`
Thumbprint string `json:"thumbprint"`
Metadata []metadata.Tuple `json:"metadata"`
Signature string `json:"signature"`
}{
Thumbprint: thumbprint,
Metadata: meta,
Signature: signature,
KeySet: keySet,
}
response := withResponse[struct {
Agent *datastore.Agent `json:"agent"`
}]()
if err := c.apiPost(ctx, "/api/v1/register", payload, &response, funcs...); err != nil {
return nil, errors.WithStack(err)
}
if response.Error != nil {
return nil, errors.WithStack(response.Error)
}
return response.Data.Agent, nil
}