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

health: Introduce health and version endpoint #90

Merged
merged 2 commits into from
Jul 21, 2018
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
56 changes: 41 additions & 15 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
# Submit coverage details
- run: test -z "$CIRCLE_PR_NUMBER" && goveralls -service=circle-ci -coverprofile=coverage.txt -repotoken=$COVERALLS_REPO_TOKEN || echo "forks are not allowed to push to coveralls"

test-docker:
build-docker:
docker:
- image: library/docker:17.10
steps:
Expand All @@ -72,7 +72,7 @@ jobs:
- run: docker build -f Dockerfile -t oathkeeper-test .
- run: docker run oathkeeper-test version

release:
release-docker:
docker:
- image: circleci/golang:1.10
working_directory: /go/src/github.com/ory/oathkeeper
Expand All @@ -86,7 +86,7 @@ jobs:
- run: docker push oryd/oathkeeper:$CIRCLE_TAG
- run: docker push oryd/oathkeeper:latest

publish-docs:
release-docs:
docker:
- image: alpine/git:1.0.4
working_directory: /go/src/github.com/ory/oathkeeper
Expand All @@ -98,7 +98,7 @@ jobs:
- run: "cp ./docs/api.swagger.json ../docs/apis/oathkeeper.json"
- run: "(cd ../docs && git add -A && git commit -a -m \"Updates ORY Oathkeeper Swagger definitions\" && git push origin) || exit 0"

changelog:
release-changelog:
docker:
- image: circleci/ruby:2.4-node
steps:
Expand All @@ -118,6 +118,19 @@ jobs:
- run: git remote add origin https://arekkas:[email protected]/ory/oathkeeper.git
- run: git push origin HEAD:master || true

release-binaries:
docker:
- image: circleci/golang:1.10
working_directory: /go/src/github.com/ory/oathkeeper
steps:
- checkout
- run: curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
- run: go get -u github.com/mitchellh/gox github.com/tcnksm/ghr
- run: dep ensure -vendor-only
- run: |
gox -parallel=2 -ldflags "-X github.com/ory/oathkeeper/cmd.Version=`git describe --tags` -X github.com/ory/oathkeeper/cmd.BuildTime=`TZ=UTC date -u '+%Y-%m-%dT%H:%M:%SZ'` -X github.com/ory/oathkeeper/cmd.GitHash=`git rev-parse HEAD`" -output "dist/{{.Dir}}-{{.OS}}-{{.Arch}}";
- run: ghr -t $GITHUB_TOKEN -u $CIRCLE_PROJECT_USERNAME -r $CIRCLE_PROJECT_REPONAME --replace `git describe --tags` dist/

workflows:
version: 2
"test, build, and relase":
Expand All @@ -130,29 +143,42 @@ workflows:
filters:
tags:
only: /.*/
- swagger:
filters:
tags:
only: /.*/
- publish-docs:
- release-docs:
filters:
branches:
only: master
- changelog:
- swagger:
filters:
branches:
only: master
- test-docker:
tags:
only: /.*/
- build-docker:
requires:
- test
- swagger
- format
filters:
tags:
only: /.*/
- release:
- release-binaries:
requires:
- build-docker
filters:
tags:
only: /.*/
branches:
ignore: /.*/
- release-docker:
requires:
- build-docker
filters:
tags:
only: /.*/
branches:
ignore: /.*/
- release-changelog:
requires:
- test-docker
- release-docker
- release-binaries
filters:
tags:
only: /.*/
Expand Down
59 changes: 59 additions & 0 deletions cmd/helper_health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright © 2017-2018 Aeneas Rekkas <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <[email protected]>
* @copyright 2017-2018 Aeneas Rekkas <[email protected]>
* @license Apache-2.0
*/

package cmd

import (
"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/ory/oathkeeper/health"
)

type pinger interface {
Ping() error
}

func newHealthHandler(database interface{}, h *herodot.JSONWriter, router *httprouter.Router) *health.Handler {
var rc health.ReadyChecker

if database == nil {
rc = func() error {
return nil
}
} else {

switch con := database.(type) {
case pinger:
rc = func() error {
return con.Ping()
}
break
default:
panic("Unknown connection type.")
}

}

handler := health.NewHandler(h, Version, map[string]health.ReadyChecker{
"database": rc,
})

return handler
}
19 changes: 16 additions & 3 deletions cmd/helper_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func connectToSql(dburl string) (*sqlx.DB, error) {
return nil, errors.Errorf(`Unknown DSN "%s" in DATABASE_URL: %s`, u.Scheme, dburl)
}

func newRuleManager(dburl string) (rule.Manager, error) {
func connectToDatabase(dburl string) (interface{}, error) {
if dburl == "memory" {
return &rule.MemoryManager{Rules: map[string]rule.Rule{}}, nil
return nil, nil
} else if dburl == "" {
return nil, errors.New("No database URL provided")
}
Expand All @@ -61,5 +61,18 @@ func newRuleManager(dburl string) (rule.Manager, error) {
return nil, errors.WithStack(err)
}

return rule.NewSQLManager(db), nil
return db, nil
}

func newRuleManager(database interface{}) (rule.Manager, error) {
if database == nil {
return &rule.MemoryManager{Rules: map[string]rule.Rule{}}, nil
}

switch db := database.(type) {
case *sqlx.DB:
return rule.NewSQLManager(db), nil
default:
return nil, errors.New("Unknown database type")
}
}
9 changes: 8 additions & 1 deletion cmd/serve_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ HTTP CONTROLS

` + corsMessage,
Run: func(cmd *cobra.Command, args []string) {
rules, err := newRuleManager(viper.GetString("DATABASE_URL"))
db, err := connectToDatabase(viper.GetString("DATABASE_URL"))
if err != nil {
logger.WithError(err).Fatalln("Unable to initialize database connectivity")
}

rules, err := newRuleManager(db)
if err != nil {
logger.WithError(err).Fatalln("Unable to connect to rule backend")
}
Expand All @@ -84,8 +89,10 @@ HTTP CONTROLS
))
keyHandler := rsakey.NewHandler(writer, keyManager)
router := httprouter.New()
health := newHealthHandler(db, writer, router)
ruleHandler.SetRoutes(router)
keyHandler.SetRoutes(router)
health.SetRoutes(router)

n := negroni.New()
n.Use(negronilogrus.NewMiddlewareFromLogger(logger, "oathkeeper-api"))
Expand Down
Loading