Commit 0d298b00 authored by Ben Kochie's avatar Ben Kochie
Browse files

Merge branch 'bjk/vendor_2.4.3' into 'master'

Update vendoring

See merge request !12
parents cc03dde2 d43c0796
Pipeline #38779826 passed with stages
in 5 minutes
image: golang:1.9
image: golang:1.11
variables:
REPO_NAME: gitlab.com/gitlab-org/prometheus-storage-migrator
......
......@@ -29,55 +29,55 @@
revision = "3a771d992973f24aa725d07868b467d1ddfceafb"
[[projects]]
digest = "1:1660bb2e30cca08494f29b5593e387c6090fbe8936970ba947185b0ca000aec0"
digest = "1:7bb1a80aeedb97f4eca27ffb775777c8d7be3be33067ad1f6808620f51e503ce"
name = "github.com/cespare/xxhash"
packages = ["."]
pruneopts = ""
revision = "5c37fe3735342a2e0d01c87a907579987c8936cc"
version = "v1.0.0"
revision = "569f7c8abf1f58d9043ab804d364483cb1c853b6"
version = "v1.1.0"
[[projects]]
digest = "1:44ec1082ba97d89ce860abcc6ee3f0cf24e658d3efb8531b0f0a52f0781e4243"
digest = "1:48e65aaf8ce34ffb3e8d56daa9417826db162afbc2040705db331e9a2e9eebe3"
name = "github.com/go-kit/kit"
packages = [
"log",
"log/level",
]
pruneopts = ""
revision = "4dc7be5d2d12881735283bcab7352178e190fc71"
version = "v0.6.0"
revision = "12210fb6ace19e0496167bb3e667dcd91fa9f69b"
version = "v0.8.0"
[[projects]]
digest = "1:6a4a01d58b227c4b6b11111b9f172ec5c17682b82724e58e6daf3f19f4faccd8"
digest = "1:df89444601379b2e1ee82bf8e6b72af9901cbeed4b469fa380a519c89c339310"
name = "github.com/go-logfmt/logfmt"
packages = ["."]
pruneopts = ""
revision = "390ab7935ee28ec6b286364bba9b4dd6410cb3d5"
version = "v0.3.0"
[[projects]]
digest = "1:9ca737b471693542351e112c9e86be9bf7385e42256893a09ecb2a98e2036f74"
name = "github.com/go-stack/stack"
packages = ["."]
pruneopts = ""
revision = "259ab82a6cad3992b4e21ff5cac294ccb06474bc"
version = "v1.7.0"
revision = "07c9b44f60d7ffdfb7d8efe1ad539965737836dc"
version = "v0.4.0"
[[projects]]
digest = "1:f958a1c137db276e52f0b50efee41a1a389dcdded59a69711f3e872757dab34b"
digest = "1:3dd078fda7500c341bc26cfbc6c6a34614f295a2457149fc1045cab767cbcf18"
name = "github.com/golang/protobuf"
packages = ["proto"]
pruneopts = ""
revision = "b4deda0973fb4c70b50d226b1af49f3da59f5265"
version = "v1.1.0"
revision = "aa810b61a9c79d51363740d207bb46cf8e620ed5"
version = "v1.2.0"
[[projects]]
branch = "master"
digest = "1:09307dfb1aa3f49a2bf869dcfa4c6c06ecd3c207221bd1c1a1141f0e51f209eb"
digest = "1:2a5888946cdbc8aa360fd43301f9fc7869d663f60d5eedae7d4e6e5e4f06f2bf"
name = "github.com/golang/snappy"
packages = ["."]
pruneopts = ""
revision = "553a641470496b2327abcac10b36396bd98e45c9"
revision = "2e65f85255dbc3072edf28d6b5b8efc472979f5a"
[[projects]]
digest = "1:6a874e3ddfb9db2b42bd8c85b6875407c702fa868eed20634ff489bc896ccfd3"
name = "github.com/konsorten/go-windows-terminal-sequences"
packages = ["."]
pruneopts = ""
revision = "5c8c8bd35d3832f5d134ae1e1e375b69a4d25242"
version = "v1.0.1"
[[projects]]
branch = "master"
......@@ -88,20 +88,20 @@
revision = "b84e30acd515aadc4b783ad4ff83aff3299bdfe0"
[[projects]]
digest = "1:81e673df85e765593a863f67cba4544cf40e8919590f04d67664940786c2b61a"
digest = "1:82b912465c1da0668582a7d1117339c278e786c2536b3c3623029a0c7141c2d0"
name = "github.com/mattn/go-runewidth"
packages = ["."]
pruneopts = ""
revision = "9e777a8366cce605130a531d2cd6363d07ad7317"
version = "v0.0.2"
revision = "ce7b0b5c7b45a81508558cd1dba6bb1e4ddb51bb"
version = "v0.0.3"
[[projects]]
digest = "1:4c23ced97a470b17d9ffd788310502a077b9c1f60221a85563e49696276b4147"
digest = "1:63722a4b1e1717be7b98fc686e0b30d5e7f734b9e93d7dee86293b6deab7ea28"
name = "github.com/matttproud/golang_protobuf_extensions"
packages = ["pbutil"]
pruneopts = ""
revision = "3247c84500bff8d9fb6d579d800f20b3e091582c"
version = "v1.0.0"
revision = "c12348ce28de40eed0136aa2b644d0ee0650e56c"
version = "v1.0.1"
[[projects]]
digest = "1:b439efedbb518b44119784e2806651f352d6a3b50f5ae1922e11c42e4890742e"
......@@ -120,12 +120,12 @@
version = "v1.0.0"
[[projects]]
digest = "1:ef57aecdf87b09455aeab4413d7e2cd15a0021fddfaa654ffb74cf266068788b"
digest = "1:897c63c623dce2d2da94aeeb096c829b63b9feeb7f53f624cadce596e3893e01"
name = "github.com/oklog/ulid"
packages = ["."]
pruneopts = ""
revision = "d311cb43c92434ec4072dfbbda3400741d0a6337"
version = "v0.3.0"
revision = "02a8604050d8466dd915307496174adb9be4593a"
version = "v1.3.1"
[[projects]]
digest = "1:78fb99d6011c2ae6c72f3293a83951311147b12b06a5ffa43abf750c4fab6ac5"
......@@ -147,24 +147,27 @@
version = "v0.8.0"
[[projects]]
digest = "1:4142d94383572e74b42352273652c62afec5b23f325222ed09198f46009022d1"
digest = "1:8b2082f564fe20dbb43a621ee0d57ae2777656ab14111d100d3d92d1b5b958b9"
name = "github.com/prometheus/client_golang"
packages = ["prometheus"]
packages = [
"prometheus",
"prometheus/internal",
]
pruneopts = ""
revision = "c5b7fccd204277076155f10851dad72b76a49317"
version = "v0.8.0"
revision = "abad2d1bd44235a26707c172eab6bca5bf2dbad3"
version = "v0.9.1"
[[projects]]
branch = "master"
digest = "1:60aca47f4eeeb972f1b9da7e7db51dee15ff6c59f7b401c1588b8e6771ba15ef"
digest = "1:185cf55b1f44a1bf243558901c3f06efa5c64ba62cfdcbb1bf7bbe8c3fb68561"
name = "github.com/prometheus/client_model"
packages = ["go"]
pruneopts = ""
revision = "99fa1f4be8e564e8a6b613da7fa6f46c9edafc6c"
revision = "5c3871d89910bfb32f5fcab2aa4b9ec68e65a99f"
[[projects]]
branch = "master"
digest = "1:b841dfec30d5d89de895adfa2314a031b2c65b402af11b89a221f2e986639c8f"
digest = "1:3015ace839b82abfb015b6fc2aebf32f4a6a8c522defacd916552387948c22a8"
name = "github.com/prometheus/common"
packages = [
"expfmt",
......@@ -173,11 +176,11 @@
"model",
]
pruneopts = ""
revision = "d811d2e9bf898806ecfb6ef6296774b13ffc314c"
revision = "4724e9255275ce38f7179b2478abeae4e28c904f"
[[projects]]
branch = "master"
digest = "1:61df0898746840afc7be5dc2c3eeec83022fab70df11ecee5b16c85e912cf5ed"
digest = "1:c95f5abc4dcdacca3649e63324cb7280f85a04b463bbef1ffb3cb137c96b1ef0"
name = "github.com/prometheus/procfs"
packages = [
".",
......@@ -186,7 +189,7 @@
"xfs",
]
pruneopts = ""
revision = "8b1c2da0d56deffdbb9e48d4414b4e674bd8083e"
revision = "619930b0b4713cc1280189bf0a4c54b3fb506f60"
[[projects]]
digest = "1:be09ac9d5731200d3f76e0a507956f596c619955f86a51bdcfc2499b6ff154c0"
......@@ -205,7 +208,7 @@
version = "v1.8.2"
[[projects]]
digest = "1:99cdacd82f0d0924b58cd6e0fc7ab986699f94556fd05687b65e170c97a24b10"
digest = "1:752ff066c1e3fb46f396f66c47571df6f0b78eeae8191032eadd539f8580b3e5"
name = "github.com/prometheus/tsdb"
packages = [
".",
......@@ -214,21 +217,22 @@
"fileutil",
"index",
"labels",
"wal",
]
pruneopts = ""
revision = "99a2c4314ff70f0673c0d07b512e2ea7a715889e"
revision = "9c8ca47399a7f53f57b440464c103f5067e9b7b6"
[[projects]]
digest = "1:8cf46b6c18a91068d446e26b67512cf16f1540b45d90b28b9533706a127f0ca6"
digest = "1:9d57e200ef5ccc4217fe0a34287308bac652435e7c6513f6263e0493d2245c56"
name = "github.com/sirupsen/logrus"
packages = ["."]
pruneopts = ""
revision = "c155da19408a8799da419ed3eeb0cb5db0ad5dbc"
version = "v1.0.5"
revision = "bcd833dfe83d3cebad139e4a29ed79cb2318bf95"
version = "v1.2.0"
[[projects]]
branch = "master"
digest = "1:3ee873561e6bc8d3b0c66967c10311a487b8c260057b09fd5fcf1d5a003da1e6"
digest = "1:61c34b9d719c7879c9381dad1db44c645529f49b23285e129a9e5f075eaf3f87"
name = "github.com/syndtr/goleveldb"
packages = [
"leveldb",
......@@ -245,35 +249,35 @@
"leveldb/util",
]
pruneopts = ""
revision = "ae970a0732be3a1f5311da86118d37b9f4bd2a5a"
revision = "b001fa50d6b27f3f0bb175a87d0cb55426d0a0ae"
[[projects]]
branch = "master"
digest = "1:a2998a82c6fa1baaa5835f4c2c6c0d6af3bf920bc8c2ebbcc329f1f2d3446d30"
digest = "1:52cfc16d4fc39fd1d779c9b52e391c7b2a31537b722d38aa9cca0f33d88d62ef"
name = "golang.org/x/crypto"
packages = ["ssh/terminal"]
pruneopts = ""
revision = "2d027ae1dddd4694d54f7a8b6cbe78dca8720226"
revision = "eb0de9b17e854e9b1ccd9963efafc79862359959"
[[projects]]
branch = "master"
digest = "1:3db92045393ef0e4c8944583f0b7ff5b873c2ce60f08cb8178911a76571a6ac0"
digest = "1:4ac199b027ed34460ec4e0a92c882156f561e78cd046fef095e50f867462435a"
name = "golang.org/x/net"
packages = ["context"]
pruneopts = ""
revision = "f73e4c9ed3b7ebdd5f699a16a880c2b1994e50dd"
revision = "adae6a3d119ae4890b46832a2e88a95adc62b8e7"
[[projects]]
branch = "master"
digest = "1:b2ea75de0ccb2db2ac79356407f8a4cd8f798fe15d41b381c00abf3ae8e55ed1"
digest = "1:88ecca26e54f601a8733c9a31d9f0883b915216a177673f0467f6b864fd0d90f"
name = "golang.org/x/sync"
packages = ["errgroup"]
pruneopts = ""
revision = "1d60e4601c6fd243af51cc01ddf169918a5407ca"
revision = "42b317875d0fa942474b76e1b46a6060d720ae6e"
[[projects]]
branch = "master"
digest = "1:c6d610a4940638d98a0dc0434c28aad91e421064af165056aec37fd619a72c1e"
digest = "1:f358024b019f87eecaadcb098113a40852c94fe58ea670ef3c3e2d2c7bd93db1"
name = "golang.org/x/sys"
packages = [
"unix",
......@@ -282,7 +286,7 @@
"windows/svc/eventlog",
]
pruneopts = ""
revision = "7dfd1290c7917b7ba22824b9d24954ab3002fe24"
revision = "4ed8d59d0b35e1e29334a206d1b3f38b1e5dfb31"
[[projects]]
digest = "1:15d017551627c8bb091bde628215b2861bed128855343fdd570c62d08871f6e1"
......@@ -293,12 +297,12 @@
version = "v2.2.6"
[[projects]]
digest = "1:03bdb73a70b30cebaf138408a2adc18ded8f489914887f86bd608f94548ddd4f"
digest = "1:7fcc8921249ff4c90b3022548d6aa8247f4d35729a7aa16ace8ed8f34ba2f352"
name = "gopkg.in/cheggaaa/pb.v1"
packages = ["."]
pruneopts = ""
revision = "1f7f140e48255d0e448ec06de47d4545b8ec1f85"
version = "v1.0.24"
revision = "007b75a044e968336a69a6c0c617251ab62ac14c"
version = "v1.0.26"
[solve-meta]
analyzer-name = "dep"
......
......@@ -23,7 +23,7 @@
[[constraint]]
name = "github.com/go-kit/kit"
version = "0.6.0"
version = "~0.8.0"
[[constraint]]
branch = "master"
......@@ -33,10 +33,14 @@
name = "github.com/prometheus/prometheus"
version = "1.8.2"
[[constraint]]
name = "github.com/prometheus/client_golang"
version = "~0.9.1"
[[constraint]]
name = "github.com/prometheus/tsdb"
revision = "99a2c4314ff70f0673c0d07b512e2ea7a715889e"
revision = "9c8ca47399a7f53f57b440464c103f5067e9b7b6" # Prometheus v2.4.3
[[constraint]]
name = "gopkg.in/cheggaaa/pb.v1"
version = "1.0.18"
version = "^1.0.26"
......@@ -453,7 +453,7 @@ func (p *persistence) cleanUpArchiveIndexes(
if has {
return nil // All good.
}
log.Warnf("Archive clean-up: Fingerprint %v is not in time-range index. Unarchiving it for recovery.")
log.Warnf("Archive clean-up: Fingerprint %v is not in time-range index. Unarchiving it for recovery.", fp)
// Again, it's fine if fp is not in the archive index.
if _, err := p.archivedFingerprintToMetrics.Delete(fp); err != nil {
return err
......
......@@ -29,12 +29,20 @@ Here are some quick benchmarks comparing the pure-Go and assembly
implementations of Sum64 against another popular Go XXH64 implementation,
[github.com/OneOfOne/xxhash](https://github.com/OneOfOne/xxhash):
| input size | OneOfOne | cespare (noasm) | cespare |
| input size | OneOfOne | cespare (purego) | cespare |
| --- | --- | --- | --- |
| 5 B | 438.34 MB/s | 596.40 MB/s | 711.11 MB/s |
| 100 B | 3676.54 MB/s | 4301.40 MB/s | 4598.95 MB/s |
| 4 KB | 8128.64 MB/s | 8840.83 MB/s | 10549.72 MB/s |
| 10 MB | 7335.19 MB/s | 7736.64 MB/s | 9024.04 MB/s |
| 5 B | 416 MB/s | 720 MB/s | 872 MB/s |
| 100 B | 3980 MB/s | 5013 MB/s | 5252 MB/s |
| 4 KB | 12727 MB/s | 12999 MB/s | 13026 MB/s |
| 10 MB | 9879 MB/s | 10775 MB/s | 10913 MB/s |
These numbers were generated with:
```
$ go test -benchtime 10s -bench '/OneOfOne,'
$ go test -tags purego -benchtime 10s -bench '/xxhash,'
$ go test -benchtime 10s -bench '/xxhash,'
```
## Projects using this package
......
module github.com/cespare/xxhash
require (
github.com/OneOfOne/xxhash v1.2.2
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72
)
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
// +build !go1.9
package xxhash
// TODO(caleb): After Go 1.10 comes out, remove this fallback code.
func rol1(x uint64) uint64 { return (x << 1) | (x >> (64 - 1)) }
func rol7(x uint64) uint64 { return (x << 7) | (x >> (64 - 7)) }
func rol11(x uint64) uint64 { return (x << 11) | (x >> (64 - 11)) }
func rol12(x uint64) uint64 { return (x << 12) | (x >> (64 - 12)) }
func rol18(x uint64) uint64 { return (x << 18) | (x >> (64 - 18)) }
func rol23(x uint64) uint64 { return (x << 23) | (x >> (64 - 23)) }
func rol27(x uint64) uint64 { return (x << 27) | (x >> (64 - 27)) }
func rol31(x uint64) uint64 { return (x << 31) | (x >> (64 - 31)) }
// +build go1.9
package xxhash
import "math/bits"
func rol1(x uint64) uint64 { return bits.RotateLeft64(x, 1) }
func rol7(x uint64) uint64 { return bits.RotateLeft64(x, 7) }
func rol11(x uint64) uint64 { return bits.RotateLeft64(x, 11) }
func rol12(x uint64) uint64 { return bits.RotateLeft64(x, 12) }
func rol18(x uint64) uint64 { return bits.RotateLeft64(x, 18) }
func rol23(x uint64) uint64 { return bits.RotateLeft64(x, 23) }
func rol27(x uint64) uint64 { return bits.RotateLeft64(x, 27) }
func rol31(x uint64) uint64 { return bits.RotateLeft64(x, 31) }
......@@ -166,22 +166,3 @@ func mergeRound(acc, val uint64) uint64 {
acc = acc*prime1 + prime4
return acc
}
// It's important for performance to get the rotates to actually compile to
// ROLQs. gc will do this for us but only if rotate amount is a constant.
//
// TODO(caleb): In Go 1.9 a single function
// rol(x uint64, k uint) uint64
// should do instead. See https://golang.org/issue/18254.
//
// TODO(caleb): In Go 1.x (1.9?) consider using the new math/bits package to be more
// explicit about things. See https://golang.org/issue/18616.
func rol1(x uint64) uint64 { return (x << 1) | (x >> (64 - 1)) }
func rol7(x uint64) uint64 { return (x << 7) | (x >> (64 - 7)) }
func rol11(x uint64) uint64 { return (x << 11) | (x >> (64 - 11)) }
func rol12(x uint64) uint64 { return (x << 12) | (x >> (64 - 12)) }
func rol18(x uint64) uint64 { return (x << 18) | (x >> (64 - 18)) }
func rol23(x uint64) uint64 { return (x << 23) | (x >> (64 - 23)) }
func rol27(x uint64) uint64 { return (x << 27) | (x >> (64 - 27)) }
func rol31(x uint64) uint64 { return (x << 31) | (x >> (64 - 31)) }
// +build !appengine
// +build gc
// +build !noasm
// +build !purego
package xxhash
......
// +build !appengine
// +build gc
// +build !noasm
// +build !purego
#include "textflag.h"
......
// +build !appengine
// +build gc
// +build !noasm
// +build !purego
package xxhash
......
// +build !amd64 appengine !gc noasm
// +build !amd64 appengine !gc purego
package xxhash
......
......@@ -12,5 +12,6 @@ script:
- ./coveralls.bash
go:
- 1.9.x
- 1.10.x
- 1.11.x
- tip
# Go kit [![Circle CI](https://circleci.com/gh/go-kit/kit.svg?style=shield)](https://circleci.com/gh/go-kit/kit) [![Travis CI](https://travis-ci.org/go-kit/kit.svg?branch=master)](https://travis-ci.org/go-kit/kit) [![GoDoc](https://godoc.org/github.com/go-kit/kit?status.svg)](https://godoc.org/github.com/go-kit/kit) [![Coverage Status](https://coveralls.io/repos/go-kit/kit/badge.svg?branch=master&service=github)](https://coveralls.io/github/go-kit/kit?branch=master) [![Go Report Card](https://goreportcard.com/badge/go-kit/kit)](https://goreportcard.com/report/go-kit/kit) [![Sourcegraph](https://sourcegraph.com/github.com/go-kit/kit/-/badge.svg)](https://sourcegraph.com/github.com/go-kit/kit?badge)
**Go kit** is a **programming toolkit** for building microservices
(or elegant monoliths) in Go. We solve common problems in distributed
**Go kit** is a **programming toolkit** for building microservices
(or elegant monoliths) in Go. We solve common problems in distributed
systems and application architecture so you can focus on delivering
business value.
......@@ -91,7 +91,7 @@ Projects with a ★ have had particular influence on Go kit's design (or vice-ve
- [mattheath/phosphor](https://github.com/mondough/phosphor), distributed system tracing
- [pivotal-golang/lager](https://github.com/pivotal-golang/lager), an opinionated logging library
- [rubyist/circuitbreaker](https://github.com/rubyist/circuitbreaker), circuit breaker library
- [Sirupsen/logrus](https://github.com/Sirupsen/logrus), structured, pluggable logging for Go ★
- [sirupsen/logrus](https://github.com/sirupsen/logrus), structured, pluggable logging for Go ★
- [sourcegraph/appdash](https://github.com/sourcegraph/appdash), application tracing system based on Google's Dapper
- [spacemonkeygo/monitor](https://github.com/spacemonkeygo/monitor), data collection, monitoring, instrumentation, and Zipkin client library
- [streadway/handy](https://github.com/streadway/handy), net/http handler filters
......@@ -107,10 +107,11 @@ Projects with a ★ have had particular influence on Go kit's design (or vice-ve
- [Martini](https://github.com/go-martini/martini)
- [Beego](http://beego.me/)
- [Revel](https://revel.github.io/) (considered [harmful](https://github.com/go-kit/kit/issues/350))
- [GoBuffalo](https://gobuffalo.io/)
## Additional reading
- [Architecting for the Cloud](http://fr.slideshare.net/stonse/architecting-for-the-cloud-using-netflixoss-codemash-workshop-29852233) — Netflix
- [Architecting for the Cloud](https://slideshare.net/stonse/architecting-for-the-cloud-using-netflixoss-codemash-workshop-29852233) — Netflix
- [Dapper, a Large-Scale Distributed Systems Tracing Infrastructure](http://research.google.com/pubs/pub36356.html) — Google
- [Your Server as a Function](http://monkey.org/~marius/funsrv.pdf) (PDF) — Twitter
......
# Roadmap
This is a coarse-grained roadmap of Go kit development direction in the short
to mid-term future. It will be kept reasonably up-to-date by the project
maintainers. Suggest new ideas, enhancements, and features using the standard
[issues](https://github.com/go-kit/kit/issues) model.
## Prioritized
1. kitgen code generation (#308, #70)
1. package pubsub (#298, #295)
## Unprioritized
- package log/levels refactor (#250, #269, #252)
- package auth/jwt (#255)
package casbin
import (
"context"
"errors"
stdcasbin "github.com/casbin/casbin"
"github.com/go-kit/kit/endpoint"
)
type contextKey string
const (
// CasbinModelContextKey holds the key to store the access control model
// in context, it can be a path to configuration file or a casbin/model
// Model.
CasbinModelContextKey contextKey = "CasbinModel"
// CasbinPolicyContextKey holds the key to store the access control policy
// in context, it can be a path to policy file or an implementation of
// casbin/persist Adapter interface.
CasbinPolicyContextKey contextKey = "CasbinPolicy"
// CasbinEnforcerContextKey holds the key to retrieve the active casbin
// Enforcer.
CasbinEnforcerContextKey contextKey = "CasbinEnforcer"
)
var (
// ErrModelContextMissing denotes a casbin model was not passed into
// the parsing of middleware's context.
ErrModelContextMissing = errors.New("CasbinModel is required in context")
// ErrPolicyContextMissing denotes a casbin policy was not passed into
// the parsing of middleware's context.
ErrPolicyContextMissing = errors.New("CasbinPolicy is required in context")
// ErrUnauthorized denotes the subject is not authorized to do the action
// intended on the given object, based on the context model and policy.
ErrUnauthorized = errors.New("Unauthorized Access")
)
// NewEnforcer checks whether the subject is authorized to do the specified
// action on the given object. If a valid access control model and policy
// is given, then the generated casbin Enforcer is stored in the context
// with CasbinEnforcer as the key.
func NewEnforcer(
subject string, object interface{}, action string,
) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (
response interface{}, err error,
) {
casbinModel := ctx.Value(CasbinModelContextKey)
casbinPolicy := ctx.Value(CasbinPolicyContextKey)
enforcer := stdcasbin.NewEnforcer(casbinModel, casbinPolicy)
ctx = context.WithValue(ctx, CasbinEnforcerContextKey, enforcer)
if !enforcer.Enforce(subject, object, action) {
return nil, ErrUnauthorized
}
return next(ctx, request)
}
}
}
package casbin
import (
"context"
"testing"
stdcasbin "github.com/casbin/casbin"
fileadapter "github.com/casbin/casbin/persist/file-adapter"
)
func TestStructBaseContext(t *testing.T) {
e := func(ctx context.Context, i interface{}) (interface{}, error) { return ctx, nil }
m := stdcasbin.NewModel()
m.AddDef("r", "r", "sub, obj, act")
m.AddDef("p", "p", "sub, obj, act")
m.AddDef("e", "e", "some(where (p.eft == allow))")
m.AddDef("m", "m", "r.sub == p.sub && keyMatch(r.obj, p.obj) && regexMatch(r.act, p.act)")
a := fileadapter.NewAdapter("testdata/keymatch_policy.csv")
ctx := context.WithValue(context.Background(), CasbinModelContextKey, m)
ctx = context.WithValue(ctx, CasbinPolicyContextKey, a)
// positive case
middleware := NewEnforcer("alice", "/alice_data/resource1", "GET")(e)
ctx1, err := middleware(ctx, struct{}{})
if err != nil {
t.Fatalf("Enforcer returned error: %s", err)
}
_, ok := ctx1.(context.Context).Value(CasbinEnforcerContextKey).(*stdcasbin.Enforcer)
if !ok {
t.Fatalf("context should contains the active enforcer")
}
// negative case
middleware = NewEnforcer("alice", "/alice_data/resource2", "POST")(e)
_, err = middleware(ctx, struct{}{})
if err == nil {
t.Fatalf("Enforcer should return error")
}