Skip to content

Commit

Permalink
only setting up admin HTTP handlers if authentication is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
rusenask committed May 24, 2019
1 parent 15e193a commit 0e73b90
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
6 changes: 6 additions & 0 deletions pkg/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type AuthResponse struct {
}

type Authenticator interface {
// indicates whether authentication is enabled
Enabled() bool
Authenticate(req *AuthRequest) (*AuthResponse, error)
GenerateToken(u User) (*AuthResponse, error)
}
Expand Down Expand Up @@ -68,6 +70,10 @@ var (
ErrUnauthorized = errors.New("unauthorized")
)

func (a *DefaultAuthenticator) Enabled() bool {
return a.opts.Username != "" && a.opts.Password != ""
}

func (a *DefaultAuthenticator) Authenticate(req *AuthRequest) (*AuthResponse, error) {

switch req.AuthType {
Expand Down
56 changes: 31 additions & 25 deletions pkg/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,38 +121,44 @@ func (s *TriggerServer) registerRoutes(mux *mux.Router) {
DebugHandler{}.AddRoutes(mux)
}

s.registerWebhookRoutes(mux)

// health endpoint for k8s to be happy
mux.HandleFunc("/healthz", s.healthHandler).Methods("GET", "OPTIONS")
// version handler
mux.HandleFunc("/version", s.versionHandler).Methods("GET", "OPTIONS")

// auth
mux.HandleFunc("/v1/auth/login", s.loginHandler).Methods("POST", "OPTIONS")
mux.HandleFunc("/v1/auth/info", s.requireAdminAuthorization(s.userInfoHandler)).Methods("GET", "OPTIONS")
mux.HandleFunc("/v1/auth/user", s.requireAdminAuthorization(s.userInfoHandler)).Methods("GET", "OPTIONS")
mux.HandleFunc("/v1/auth/logout", s.requireAdminAuthorization(s.logoutHandler)).Methods("POST", "GET", "OPTIONS")
mux.HandleFunc("/v1/auth/refresh", s.requireAdminAuthorization(s.refreshHandler)).Methods("GET", "OPTIONS")

// approvals
mux.HandleFunc("/v1/approvals", s.requireAdminAuthorization(s.approvalsHandler)).Methods("GET", "OPTIONS")
// approving/rejecting
mux.HandleFunc("/v1/approvals", s.requireAdminAuthorization(s.approvalApproveHandler)).Methods("POST", "OPTIONS")
// updating required approvals count
mux.HandleFunc("/v1/approvals", s.requireAdminAuthorization(s.approvalSetHandler)).Methods("PUT", "OPTIONS")

// available resources
mux.HandleFunc("/v1/resources", s.requireAdminAuthorization(s.resourcesHandler)).Methods("GET", "OPTIONS")

mux.HandleFunc("/v1/policies", s.requireAdminAuthorization(s.policyUpdateHandler)).Methods("PUT", "OPTIONS")

// tracked images
mux.HandleFunc("/v1/tracked", s.requireAdminAuthorization(s.trackedHandler)).Methods("GET", "OPTIONS")
mux.HandleFunc("/v1/audit", s.requireAdminAuthorization(s.adminAuditLogHandler)).Methods("GET", "OPTIONS")
mux.HandleFunc("/v1/stats", s.requireAdminAuthorization(s.statsHandler)).Methods("GET", "OPTIONS")

mux.Handle("/metrics", promhttp.Handler())

s.registerWebhookRoutes(mux)
if s.authenticator.Enabled() {
log.Info("authentication enabled, setting up admin HTTP handlers")
// auth
mux.HandleFunc("/v1/auth/login", s.loginHandler).Methods("POST", "OPTIONS")
mux.HandleFunc("/v1/auth/info", s.requireAdminAuthorization(s.userInfoHandler)).Methods("GET", "OPTIONS")
mux.HandleFunc("/v1/auth/user", s.requireAdminAuthorization(s.userInfoHandler)).Methods("GET", "OPTIONS")
mux.HandleFunc("/v1/auth/logout", s.requireAdminAuthorization(s.logoutHandler)).Methods("POST", "GET", "OPTIONS")
mux.HandleFunc("/v1/auth/refresh", s.requireAdminAuthorization(s.refreshHandler)).Methods("GET", "OPTIONS")

// approvals
mux.HandleFunc("/v1/approvals", s.requireAdminAuthorization(s.approvalsHandler)).Methods("GET", "OPTIONS")
// approving/rejecting
mux.HandleFunc("/v1/approvals", s.requireAdminAuthorization(s.approvalApproveHandler)).Methods("POST", "OPTIONS")
// updating required approvals count
mux.HandleFunc("/v1/approvals", s.requireAdminAuthorization(s.approvalSetHandler)).Methods("PUT", "OPTIONS")

// available resources
mux.HandleFunc("/v1/resources", s.requireAdminAuthorization(s.resourcesHandler)).Methods("GET", "OPTIONS")

mux.HandleFunc("/v1/policies", s.requireAdminAuthorization(s.policyUpdateHandler)).Methods("PUT", "OPTIONS")

// tracked images
mux.HandleFunc("/v1/tracked", s.requireAdminAuthorization(s.trackedHandler)).Methods("GET", "OPTIONS")
mux.HandleFunc("/v1/audit", s.requireAdminAuthorization(s.adminAuditLogHandler)).Methods("GET", "OPTIONS")
mux.HandleFunc("/v1/stats", s.requireAdminAuthorization(s.statsHandler)).Methods("GET", "OPTIONS")
} else {
log.Info("authentication is not enabled, admin HTTP handlers are not initialized")
}

}

func (s *TriggerServer) registerWebhookRoutes(mux *mux.Router) {
Expand Down

0 comments on commit 0e73b90

Please sign in to comment.