Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add authentication with API key for the /logs endpoint #663

Merged
merged 2 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1143,8 +1143,6 @@ golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492 h1:Paq34FxTluEPvVyayQqMPgHm+vTOrIifmcYxFBx9TLg=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210415045647-66c3f260301c h1:6L+uOeS3OQt/f4eFHXZcTxeZrGCuz+CLElgEBjbcTA4=
golang.org/x/sys v0.0.0-20210415045647-66c3f260301c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
2 changes: 1 addition & 1 deletion pkg/http-server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (g *APIServer) Routes(configFile string) []*Route {
{verb: "POST", path: versionedPath("/{iac}/{iacVersion}/{cloud}/remote/dir/scan"), fn: h.scanRemoteRepo},

// k8s webhook Routes
{verb: "GET", path: "/k8s/webhooks/logs", fn: h.getLogs},
{verb: "GET", path: "/k8s/webhooks/{apiKey}/logs", fn: h.getLogs},
patilpankaj212 marked this conversation as resolved.
Show resolved Hide resolved
{verb: "GET", path: "/k8s/webhooks/logs/{uid}", fn: h.getLogByUID},
{verb: "POST", path: versionedPath("/k8s/webhooks/{apiKey}/scan/validate"), fn: h.validateK8SWebhook},
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/http-server/webhook-scan-logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/accurics/terrascan/pkg/config"
admissionWebhook "github.com/accurics/terrascan/pkg/k8s/admission-webhook"
"github.com/accurics/terrascan/pkg/k8s/dblogs"
"github.com/accurics/terrascan/pkg/results"
"github.com/gorilla/mux"
Expand Down Expand Up @@ -65,6 +66,25 @@ type webhookDisplayedShowLog struct {

func (g *APIHandler) getLogs(w http.ResponseWriter, r *http.Request) {

var (
params = mux.Vars(r)
apiKey = params["apiKey"]
)

// Validate if authorized (API key is specified and matched the server one (saved in an environment variable)
validatingWebhook := admissionWebhook.NewValidatingWebhook(g.configFile, []byte(""))
if err := validatingWebhook.Authorize(apiKey); err != nil {
switch err {
case admissionWebhook.ErrAPIKeyMissing:
apiErrorResponse(w, err.Error(), http.StatusBadRequest)
case admissionWebhook.ErrUnauthorized:
apiErrorResponse(w, err.Error(), http.StatusUnauthorized)
default:
apiErrorResponse(w, err.Error(), http.StatusInternalServerError)
}
return
}

// Return an HTML page including all the logs history
logger := dblogs.NewWebhookScanLogger()

Expand Down