-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Amir Malka <amirm@armosec.io>
- Loading branch information
Showing
10 changed files
with
134 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package authentication | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/kubescape/go-logger" | ||
"github.com/kubescape/go-logger/helpers" | ||
"github.com/kubescape/synchronizer/config" | ||
"github.com/kubescape/synchronizer/core" | ||
"github.com/kubescape/synchronizer/domain" | ||
) | ||
|
||
func AuthenticationServerMiddleware(client *http.Client, cfg config.AuthenticationServerConfig, next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
||
accessKey := r.Header.Get(core.AccessKeyHeader) | ||
account := r.Header.Get(core.AccountHeader) | ||
cluster := r.Header.Get(core.ClusterNameHeader) | ||
|
||
if accessKey == "" || account == "" || cluster == "" { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
if client != nil { | ||
u, err := url.Parse(cfg.Url) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// copy headers to authentication request query params (configurable) | ||
q := u.Query() | ||
for header, queryParam := range cfg.HeaderToQueryParamMapping { | ||
q.Set(queryParam, r.Header.Get(header)) | ||
} | ||
u.RawQuery = q.Encode() | ||
|
||
authenticationRequest, err := http.NewRequestWithContext(r.Context(), http.MethodGet, u.String(), nil) | ||
if err != nil { | ||
logger.L().Error("unable to create authentication request", helpers.Error(err)) | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
|
||
for origin, dest := range cfg.HeaderToHeaderMapping { | ||
authenticationRequest.Header.Set(dest, r.Header.Get(origin)) | ||
} | ||
|
||
response, err := client.Do(authenticationRequest) | ||
if err != nil || response.StatusCode != http.StatusOK { | ||
w.WriteHeader(http.StatusUnauthorized) | ||
return | ||
} | ||
} | ||
|
||
// create new context with client identifier | ||
ctx := context.WithValue(r.Context(), domain.ContextKeyClientIdentifier, domain.ClientIdentifier{ | ||
Account: account, | ||
Cluster: cluster, | ||
}) | ||
// create new request using the new context | ||
authenticatedRequest := r.WithContext(ctx) | ||
next.ServeHTTP(w, authenticatedRequest) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
package core | ||
|
||
import ( | ||
beServerV1 "github.com/kubescape/backend/pkg/server/v1" | ||
) | ||
|
||
// These headers are required for client-server authentication | ||
const ( | ||
AccessKeyHeader = beServerV1.AccessKeyHeader | ||
AccountHeader = "Account" | ||
ClusterNameHeader = "ClusterName" | ||
AccessKeyHeader = "X-API-KEY" | ||
AccountHeader = "X-API-ACCOUNT" | ||
ClusterNameHeader = "X-API-CLUSTER" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package domain | ||
|
||
type contextKey string | ||
|
||
const ( | ||
ContextKeyAccount = "account" | ||
ContextKeyAccessKey = "accessKey" | ||
ContextKeyClusterName = "clusterName" | ||
ContextKeyDepth = "depth" | ||
ContextKeyMsgId = "msgId" | ||
ContextKeyClientIdentifier contextKey = "clientIdentifier" | ||
ContextKeyDepth contextKey = "depth" | ||
ContextKeyMsgId contextKey = "msgId" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters