mirror of
https://github.com/portainer/portainer.git
synced 2025-07-25 16:29:44 +02:00
* refactor(bolt): move ErrObjectNotFound to bolt * refactor(http): move ErrUnauthorized to http package * refactor(http): move ErrResourceAccessDenied to http errors * refactor(http): move security errors to package * refactor(users): move user errors to users package * refactor(errors): move single errors to their package * refactor(schedules): move schedule error to package * refactor(http): move endpoint error to http package * refactor(docker): move docker errors to package * refactor(filesystem): move filesystem errors to package * refactor(errors): remove portainer.Error * style(chisel): reorder imports * fix(stacks): remove portainer.Error
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package security
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
)
|
|
|
|
const (
|
|
defaultHTTPTimeout = 5
|
|
)
|
|
|
|
type rbacExtensionClient struct {
|
|
httpClient *http.Client
|
|
extensionURL string
|
|
licenseKey string
|
|
}
|
|
|
|
func newRBACExtensionClient(extensionURL string) *rbacExtensionClient {
|
|
return &rbacExtensionClient{
|
|
extensionURL: extensionURL,
|
|
httpClient: &http.Client{
|
|
Timeout: time.Second * time.Duration(defaultHTTPTimeout),
|
|
},
|
|
}
|
|
}
|
|
|
|
func (client *rbacExtensionClient) setLicenseKey(licenseKey string) {
|
|
client.licenseKey = licenseKey
|
|
}
|
|
|
|
func (client *rbacExtensionClient) checkAuthorization(authRequest *portainer.APIOperationAuthorizationRequest) error {
|
|
encodedAuthRequest, err := json.Marshal(authRequest)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", client.extensionURL+"/authorized_operation", nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req.Header.Set("X-RBAC-AuthorizationRequest", string(encodedAuthRequest))
|
|
req.Header.Set("X-PortainerExtension-License", client.licenseKey)
|
|
|
|
resp, err := client.httpClient.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusNoContent {
|
|
return ErrAuthorizationRequired
|
|
}
|
|
|
|
return nil
|
|
}
|