2021-09-24 04:56:22 +00:00
|
|
|
package agent
|
2021-01-25 19:16:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
)
|
|
|
|
|
2022-10-14 22:42:31 +00:00
|
|
|
// Transport is an http.Transport wrapper that adds custom http headers to communicate to an Agent
|
|
|
|
type Transport struct {
|
|
|
|
httpTransport *http.Transport
|
|
|
|
signatureService portainer.DigitalSignatureService
|
|
|
|
}
|
2021-01-25 19:16:53 +00:00
|
|
|
|
2021-09-24 04:56:22 +00:00
|
|
|
// NewTransport returns a new transport that can be used to send signed requests to a Portainer agent
|
|
|
|
func NewTransport(signatureService portainer.DigitalSignatureService, httpTransport *http.Transport) *Transport {
|
|
|
|
transport := &Transport{
|
2021-01-25 19:16:53 +00:00
|
|
|
httpTransport: httpTransport,
|
|
|
|
signatureService: signatureService,
|
|
|
|
}
|
|
|
|
|
|
|
|
return transport
|
|
|
|
}
|
|
|
|
|
|
|
|
// RoundTrip is the implementation of the the http.RoundTripper interface
|
2021-09-24 04:56:22 +00:00
|
|
|
func (transport *Transport) RoundTrip(request *http.Request) (*http.Response, error) {
|
2021-01-25 19:16:53 +00:00
|
|
|
signature, err := transport.signatureService.CreateSignature(portainer.PortainerAgentSignatureMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Header.Set(portainer.PortainerAgentPublicKeyHeader, transport.signatureService.EncodedPublicKey())
|
|
|
|
request.Header.Set(portainer.PortainerAgentSignatureHeader, signature)
|
|
|
|
|
|
|
|
return transport.httpTransport.RoundTrip(request)
|
|
|
|
}
|