2023-02-02 10:55:24 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/metadata"
|
2023-02-02 10:55:24 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/datastore"
|
2023-03-02 13:05:24 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/jwk"
|
2023-02-02 10:55:24 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
func (c *Client) RegisterAgent(ctx context.Context, key jwk.Key, thumbprint string, meta []metadata.Tuple, funcs ...OptionFunc) (*datastore.Agent, error) {
|
2023-03-02 13:05:24 +01:00
|
|
|
keySet, err := jwk.PublicKeySet(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
signature, err := jwk.Sign(key, thumbprint, meta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-02-02 10:55:24 +01:00
|
|
|
payload := struct {
|
2023-03-02 13:05:24 +01:00
|
|
|
KeySet jwk.Set `json:"keySet"`
|
|
|
|
Thumbprint string `json:"thumbprint"`
|
|
|
|
Metadata []metadata.Tuple `json:"metadata"`
|
|
|
|
Signature string `json:"signature"`
|
2023-02-02 10:55:24 +01:00
|
|
|
}{
|
2023-03-02 13:05:24 +01:00
|
|
|
Thumbprint: thumbprint,
|
|
|
|
Metadata: meta,
|
|
|
|
Signature: signature,
|
|
|
|
KeySet: keySet,
|
2023-02-02 10:55:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
response := withResponse[struct {
|
|
|
|
Agent *datastore.Agent `json:"agent"`
|
|
|
|
}]()
|
|
|
|
|
2023-03-07 23:10:42 +01:00
|
|
|
if err := c.apiPost(ctx, "/api/v1/register", payload, &response, funcs...); err != nil {
|
2023-02-02 10:55:24 +01:00
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Error != nil {
|
|
|
|
return nil, errors.WithStack(response.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.Data.Agent, nil
|
|
|
|
}
|