From 208aa5366b0905806bdd07131daae3b8a5b0a8f4 Mon Sep 17 00:00:00 2001 From: Aeneas Date: Sun, 4 Jun 2017 16:49:55 +0200 Subject: [PATCH] vendor: improve vendor dependencies (#67) * vendor: remove stray github.com/Sirupsen/logrus * vendor: remove ory/common --- compiler/README.md | 3 + compiler/regex.go | 125 +++++++++++++++ compiler/regex_test.go | 43 ++++++ errors.go | 7 + glide.lock | 66 ++++---- glide.yaml | 17 +- integration/integration.go | 146 ++++++++++++++++++ manager/sql/manager_sql.go | 2 +- .../sql/manager_sql_migration_0_5_to_0_6.go | 6 +- manager_all_test.go | 2 +- matcher_regexp.go | 2 +- 11 files changed, 366 insertions(+), 53 deletions(-) create mode 100644 compiler/README.md create mode 100644 compiler/regex.go create mode 100644 compiler/regex_test.go create mode 100644 integration/integration.go diff --git a/compiler/README.md b/compiler/README.md new file mode 100644 index 0000000..294b04d --- /dev/null +++ b/compiler/README.md @@ -0,0 +1,3 @@ +# compiler + +[Read the docs.](https://godoc.org/github.com/ory-am/common/compiler) \ No newline at end of file diff --git a/compiler/regex.go b/compiler/regex.go new file mode 100644 index 0000000..1f6ac30 --- /dev/null +++ b/compiler/regex.go @@ -0,0 +1,125 @@ +// Package compiler offers a regexp compiler which compiles regex templates to regexp.Regexp +// +// reg, err := compiler.CompileRegex("foo:bar.baz:<[0-9]{2,10}>", '<', '>') +// // if err != nil ... +// reg.MatchString("foo:bar.baz:123") +// +// reg, err := compiler.CompileRegex("/foo/bar/url/{[a-z]+}", '{', '}') +// // if err != nil ... +// reg.MatchString("/foo/bar/url/abz") +// +// This package is adapts github.com/gorilla/mux/regexp.go +package compiler + +// Copyright 2012 The Gorilla Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license as follows: + +//Copyright (c) 2012 Rodrigo Moraes. All rights reserved. +// +//Redistribution and use in source and binary forms, with or without +//modification, are permitted provided that the following conditions are +//met: +// +//* Redistributions of source code must retain the above copyright +//notice, this list of conditions and the following disclaimer. +//* Redistributions in binary form must reproduce the above +//copyright notice, this list of conditions and the following disclaimer +//in the documentation and/or other materials provided with the +//distribution. +//* Neither the name of Google Inc. nor the names of its +//contributors may be used to endorse or promote products derived from +//this software without specific prior written permission. +// +//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +//A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +//OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +//LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +//DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +//THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +//(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +//OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +import ( + "bytes" + "fmt" + "regexp" +) + +// delimiterIndices returns the first level delimiter indices from a string. +// It returns an error in case of unbalanced delimiters. +func delimiterIndices(s string, delimiterStart, delimiterEnd byte) ([]int, error) { + var level, idx int + idxs := make([]int, 0) + for i := 0; i < len(s); i++ { + switch s[i] { + case delimiterStart: + if level++; level == 1 { + idx = i + } + case delimiterEnd: + if level--; level == 0 { + idxs = append(idxs, idx, i+1) + } else if level < 0 { + return nil, fmt.Errorf(`Unbalanced braces in "%q"`, s) + } + } + } + + if level != 0 { + return nil, fmt.Errorf(`Unbalanced braces in "%q"`, s) + } + + return idxs, nil +} + +// CompileRegex parses a template and returns a Regexp. +// +// You can define your own delimiters. It is e.g. common to use curly braces {} but I recommend using characters +// which have no special meaning in Regex, e.g.: <, > +// +// reg, err := compiler.CompileRegex("foo:bar.baz:<[0-9]{2,10}>", '<', '>') +// // if err != nil ... +// reg.MatchString("foo:bar.baz:123") +func CompileRegex(tpl string, delimiterStart, delimiterEnd byte) (*regexp.Regexp, error) { + // Check if it is well-formed. + idxs, errBraces := delimiterIndices(tpl, delimiterStart, delimiterEnd) + if errBraces != nil { + return nil, errBraces + } + varsR := make([]*regexp.Regexp, len(idxs)/2) + pattern := bytes.NewBufferString("") + pattern.WriteByte('^') + + var end int + var err error + for i := 0; i < len(idxs); i += 2 { + // Set all values we are interested in. + raw := tpl[end:idxs[i]] + end = idxs[i+1] + patt := tpl[idxs[i]+1 : end-1] + // Build the regexp pattern. + varIdx := i / 2 + fmt.Fprintf(pattern, "%s(%s)", regexp.QuoteMeta(raw), patt) + varsR[varIdx], err = regexp.Compile(fmt.Sprintf("^%s$", patt)) + if err != nil { + return nil, err + } + } + + // Add the remaining. + raw := tpl[end:] + pattern.WriteString(regexp.QuoteMeta(raw)) + pattern.WriteByte('$') + + // Compile full regexp. + reg, errCompile := regexp.Compile(pattern.String()) + if errCompile != nil { + return nil, errCompile + } + + return reg, nil +} diff --git a/compiler/regex_test.go b/compiler/regex_test.go new file mode 100644 index 0000000..0a71a04 --- /dev/null +++ b/compiler/regex_test.go @@ -0,0 +1,43 @@ +package compiler + +import ( + "regexp" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRegexCompiler(t *testing.T) { + for k, c := range []struct { + template string + delimiterStart byte + delimiterEnd byte + failCompile bool + matchAgainst string + failMatch bool + }{ + {"urn:foo:{.*}", '{', '}', false, "urn:foo:bar:baz", false}, + {"urn:foo.bar.com:{.*}", '{', '}', false, "urn:foo.bar.com:bar:baz", false}, + {"urn:foo.bar.com:{.*}", '{', '}', false, "urn:foo.com:bar:baz", true}, + {"urn:foo.bar.com:{.*}", '{', '}', false, "foobar", true}, + {"urn:foo.bar.com:{.{1,2}}", '{', '}', false, "urn:foo.bar.com:aa", false}, + + {"urn:foo.bar.com:{.*{}", '{', '}', true, "", true}, + {"urn:foo:<.*>", '<', '>', false, "urn:foo:bar:baz", false}, + + // Ignoring this case for now... + //{"urn:foo.bar.com:{.*\\{}", '{', '}', false, "", true}, + } { + k++ + result, err := CompileRegex(c.template, c.delimiterStart, c.delimiterEnd) + assert.Equal(t, c.failCompile, err != nil, "Case %d", k) + if c.failCompile || err != nil { + continue + } + + t.Logf("Case %d compiled to: %s", k, result.String()) + ok, err := regexp.MatchString(result.String(), c.matchAgainst) + assert.Nil(t, err, "Case %d", k) + assert.Equal(t, !c.failMatch, ok, "Case %d", k) + } +} diff --git a/errors.go b/errors.go index d0cc02e..c6f08dd 100644 --- a/errors.go +++ b/errors.go @@ -22,6 +22,13 @@ var ( status: http.StatusText(http.StatusForbidden), reason: "The request was denied because a policy denied request.", }) + + // ErrNotFound is returned when a resource can not be found. + ErrNotFound = errors.WithStack(&errorWithContext{ + error: errors.New("Resource could not be found"), + code: http.StatusNotFound, + status: http.StatusText(http.StatusNotFound), + }) ) func NewErrResourceNotFound(err error) error { diff --git a/glide.lock b/glide.lock index f03867b..09cf383 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: b5611fae89e4bbfa1d5e532daff04ba990d66fa67e7cf813a2048e1b302981da -updated: 2017-05-03T15:35:29.9729739+02:00 +hash: 79d55f9cedd82df3cdc2dcfcd338e9150bdbb0de19115a2dc3fc971408bc7dd3 +updated: 2017-06-04T16:44:58.060005+02:00 imports: - name: github.com/Azure/go-ansiterm version: fa152c58bc15761d0200cb75fe958b89a9d4888e @@ -8,11 +8,7 @@ imports: - name: github.com/cenk/backoff version: 32cd0c5b3aef12c76ed64aaf678f6c79736be7dc - name: github.com/cenkalti/backoff - version: b02f2bbce11d7ea6b97f282ef1771b0fe2f65ef3 -- name: github.com/davecgh/go-spew - version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9 - subpackages: - - spew + version: 5d150e7eec023ce7a124856b37c68e54b4050ac7 - name: github.com/docker/docker version: 4845c567eb35d68f35b0b1713a09b0c8d47fe67e subpackages: @@ -48,11 +44,9 @@ imports: - name: github.com/docker/go-units version: 0dadbb0345b35ec7ef35e228dabb8de89a65bf52 - name: github.com/fsouza/go-dockerclient - version: 30d142bbfdec74e09ecb4bdb89a44440ae4662ac -- name: github.com/go-errors/errors - version: 8fa88b06e5974e97fbf9899a7f86a344bfd1f105 + version: c933ed18bef34ec2955de03de8ef9a3bb996e3df - name: github.com/go-redis/redis - version: 62ecc61210cb7dd8ca9aebb8bebbc849c358383b + version: 663bb76bcc3763c41877b63671003705a47289c5 subpackages: - internal - internal/consistenthash @@ -62,7 +56,7 @@ imports: - name: github.com/go-sql-driver/mysql version: a0583e0143b1624142adab07e0e97fe106d99561 - name: github.com/golang/protobuf - version: 69b215d01a5606c843240eab4937eab3acee6530 + version: 5a0f697c9ed9d68fef0116532c6e05cfeae00e55 subpackages: - proto - name: github.com/hailocab/go-hostpool @@ -74,60 +68,43 @@ imports: subpackages: - simplelru - name: github.com/jmoiron/sqlx - version: f4076845477b10ac2453a16377a8383467aafe72 + version: d9bd385d68c068f1fabb5057e3dedcbcbb039d0f subpackages: - reflectx - name: github.com/lib/pq - version: ba5d4f7a35561e22fbdf7a39aa0070f4d460cfc0 + version: 8837942c3e09574accbc5f150e2c5e057189cace subpackages: - oid - name: github.com/Microsoft/go-winio - version: f3b1913901892ada21d52d0cffe1d4c7080d36b7 + version: f533f7a102197536779ea3a8cb881d639e21ec5a - name: github.com/Nvveen/Gotty version: cd527374f1e5bff4938207604a14f2e38a9cf512 -- name: github.com/oleiade/reflections - version: 2b6ec3da648e3e834dc41bad8d9ed7f2dc6a9496 - name: github.com/opencontainers/runc - version: efb2bc3fb0e36b2ec0d2c44b460bfc33a26a0bef + version: 37234959723df2627fb3a22f174f01378f37fb7e subpackages: - libcontainer/system - libcontainer/user -- name: github.com/ory-am/common - version: b6357395e30805e2ad1f6d8fb759fa2b7146d8da - subpackages: - - compiler - - integration - - pkg -- name: github.com/ory-am/dockertest +- name: github.com/ory/dockertest version: 9d0647ae761f96a6738c5afb49688d22979b21ff - name: github.com/pkg/errors version: 645ef00459ed84a119197bfb8d8205042c6df63d -- name: github.com/pmezard/go-difflib - version: d8ed2627bdf02c080bf22230dbb337003b7aba2d - subpackages: - - difflib - name: github.com/rubenv/sql-migrate - version: f64b6080c334adaf843209164107439e92bb170b + version: 42c30aabf72921b83678a3d0680fd6cd46682f1b subpackages: - sqlparse - name: github.com/Sirupsen/logrus version: ba1b36c82c5e05c4f912a88eab0dcd91a171688f -- name: github.com/stretchr/testify - version: 69483b4bd14f5845b5a1e55bca19e954e827f1d0 - subpackages: - - assert - - require - name: golang.org/x/crypto - version: 453249f01cfeb54c3d549ddb75ff152ca243f9d8 + version: e1a4589e7d3ea14a3352255d04b6f1a418845e5e subpackages: - pbkdf2 - name: golang.org/x/net - version: dd2d9a67c97da0afa00d5726e28086007a0acce5 + version: e4fa1c5465ad6111f206fc92186b8c83d64adbe1 subpackages: - context - context/ctxhttp - name: golang.org/x/sys - version: e4594059fe4cde2daf423055a596c2cd1e6c9adf + version: b90f89a1e7a9c1f6b918820b3daa7f08488c8594 subpackages: - unix - windows @@ -142,9 +119,22 @@ imports: - name: gopkg.in/gorp.v1 version: c87af80f3cc5036b55b83d77171e156791085e2e testImports: +- name: github.com/davecgh/go-spew + version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9 + subpackages: + - spew - name: github.com/golang/mock version: bd3c8e81be01eef76d4b503f5e687d2d1354d2d9 subpackages: - gomock - name: github.com/pborman/uuid version: a97ce2ca70fa5a848076093f05e639a89ca34d06 +- name: github.com/pmezard/go-difflib + version: d8ed2627bdf02c080bf22230dbb337003b7aba2d + subpackages: + - difflib +- name: github.com/stretchr/testify + version: 69483b4bd14f5845b5a1e55bca19e954e827f1d0 + subpackages: + - assert + - require diff --git a/glide.yaml b/glide.yaml index c4e3fc4..fa7abda 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,22 +1,23 @@ package: github.com/ory/ladon import: +- package: github.com/go-redis/redis + version: ~6.4.1 +- package: github.com/go-sql-driver/mysql + version: ~1.3.0 - package: github.com/hashicorp/golang-lru - package: github.com/jmoiron/sqlx -- package: github.com/ory-am/common - version: ~0.4.0 - subpackages: - - compiler - - pkg +- package: github.com/lib/pq +- package: github.com/ory/dockertest + version: ~3.0.7 - package: github.com/pkg/errors version: ~0.8.0 - package: github.com/rubenv/sql-migrate +- package: gopkg.in/gorethink/gorethink.v3 + version: ~3.0.2 testImport: -- package: github.com/go-sql-driver/mysql - version: ~1.3.0 - package: github.com/golang/mock subpackages: - gomock -- package: github.com/lib/pq - package: github.com/pborman/uuid version: ~1.0.0 - package: github.com/stretchr/testify diff --git a/integration/integration.go b/integration/integration.go new file mode 100644 index 0000000..56c2575 --- /dev/null +++ b/integration/integration.go @@ -0,0 +1,146 @@ +package integration + +import ( + "fmt" + "log" + "time" + + "github.com/go-redis/redis" + _ "github.com/go-sql-driver/mysql" + "github.com/jmoiron/sqlx" + _ "github.com/lib/pq" + "github.com/ory/dockertest" + r "gopkg.in/gorethink/gorethink.v3" +) + +var resources []*dockertest.Resource +var pool *dockertest.Pool + +func KillAll() { + for _, resource := range resources { + pool.Purge(resource) + } + resources = []*dockertest.Resource{} +} + +func ConnectToMySQL() *sqlx.DB { + var db *sqlx.DB + var err error + pool, err = dockertest.NewPool("") + pool.MaxWait = time.Minute * 2 + if err != nil { + log.Fatalf("Could not connect to docker: %s", err) + } + + resource, err := pool.Run("mysql", "5.7", []string{"MYSQL_ROOT_PASSWORD=secret"}) + if err != nil { + log.Fatalf("Could not start resource: %s", err) + } + + if err = pool.Retry(func() error { + var err error + db, err = sqlx.Open("mysql", fmt.Sprintf("root:secret@(localhost:%s)/mysql?parseTime=true", resource.GetPort("3306/tcp"))) + if err != nil { + return err + } + return db.Ping() + }); err != nil { + log.Fatalf("Could not connect to docker: %s", err) + } + + resources = append(resources, resource) + return db +} + +func ConnectToPostgres(database string) *sqlx.DB { + var db *sqlx.DB + var err error + pool, err = dockertest.NewPool("") + if err != nil { + log.Fatalf("Could not connect to docker: %s", err) + } + + resource, err := pool.Run("postgres", "9.6", []string{"POSTGRES_PASSWORD=secret", "POSTGRES_DB=" + database}) + if err != nil { + log.Fatalf("Could not start resource: %s", err) + } + + if err = pool.Retry(func() error { + var err error + db, err = sqlx.Open("postgres", fmt.Sprintf("postgres://postgres:secret@localhost:%s/%s?sslmode=disable", resource.GetPort("5432/tcp"), database)) + if err != nil { + return err + } + return db.Ping() + }); err != nil { + log.Fatalf("Could not connect to docker: %s", err) + } + + resources = append(resources, resource) + return db +} + +func ConnectToRethinkDB(database string, tables ...string) *r.Session { + var session *r.Session + var err error + pool, err = dockertest.NewPool("") + if err != nil { + log.Fatalf("Could not connect to docker: %s", err) + } + + resource, err := pool.Run("rethinkdb", "2.3", nil) + if err != nil { + log.Fatalf("Could not start resource: %s", err) + } + + if err = pool.Retry(func() error { + if session, err = r.Connect(r.ConnectOpts{Address: fmt.Sprintf("localhost:%s", resource.GetPort("28015/tcp")), Database: database}); err != nil { + return err + } else if _, err = r.DBCreate(database).RunWrite(session); err != nil { + log.Printf("Database exists: %s", err) + return err + } + + for _, table := range tables { + if _, err = r.TableCreate(table).RunWrite(session); err != nil { + log.Printf("Could not create table: %s", err) + return err + } + } + + time.Sleep(100 * time.Millisecond) + return nil + }); err != nil { + log.Fatalf("Could not connect to docker: %s", err) + } + + resources = append(resources, resource) + return session +} + +func ConnectToRedis() *redis.Client { + var db *redis.Client + var err error + pool, err = dockertest.NewPool("") + if err != nil { + log.Fatalf("Could not connect to docker: %s", err) + } + + resource, err := pool.Run("redis", "3.2", nil) + if err != nil { + log.Fatalf("Could not start resource: %s", err) + } + + if err = pool.Retry(func() error { + db = redis.NewClient(&redis.Options{ + Addr: fmt.Sprintf("localhost:%s", resource.GetPort("6379/tcp")), + }) + + return db.Ping().Err() + }); err != nil { + log.Fatalf("Could not connect to docker: %s", err) + } + + resources = append(resources, resource) + return db +} diff --git a/manager/sql/manager_sql.go b/manager/sql/manager_sql.go index 293b4e9..1a67c1c 100644 --- a/manager/sql/manager_sql.go +++ b/manager/sql/manager_sql.go @@ -8,7 +8,7 @@ import ( "strings" "github.com/jmoiron/sqlx" - "github.com/ory-am/common/compiler" + "github.com/ory/ladon/compiler" . "github.com/ory/ladon" "github.com/pkg/errors" "github.com/rubenv/sql-migrate" diff --git a/manager/sql/manager_sql_migration_0_5_to_0_6.go b/manager/sql/manager_sql_migration_0_5_to_0_6.go index 1e447c2..2092663 100644 --- a/manager/sql/manager_sql_migration_0_5_to_0_6.go +++ b/manager/sql/manager_sql_migration_0_5_to_0_6.go @@ -4,12 +4,10 @@ import ( "database/sql" "encoding/json" "fmt" - "log" "github.com/jmoiron/sqlx" - "github.com/ory-am/common/compiler" - "github.com/ory-am/common/pkg" + "github.com/ory/ladon/compiler" "github.com/ory/ladon" . "github.com/ory/ladon" "github.com/pkg/errors" @@ -86,7 +84,7 @@ func getLinkedSQL(db *sqlx.DB, table, policy string) ([]string, error) { urns := []string{} rows, err := db.Query(db.Rebind(fmt.Sprintf("SELECT template FROM %s WHERE policy=?", table)), policy) if err == sql.ErrNoRows { - return nil, errors.Wrap(pkg.ErrNotFound, "") + return nil, errors.Wrap(ladon.ErrNotFound, "") } else if err != nil { return nil, errors.WithStack(err) } diff --git a/manager_all_test.go b/manager_all_test.go index 692cd3a..fd45ab1 100644 --- a/manager_all_test.go +++ b/manager_all_test.go @@ -9,7 +9,7 @@ import ( _ "github.com/go-sql-driver/mysql" _ "github.com/lib/pq" - "github.com/ory-am/common/integration" + "github.com/ory/ladon/integration" . "github.com/ory/ladon" . "github.com/ory/ladon/manager/memory" . "github.com/ory/ladon/manager/sql" diff --git a/matcher_regexp.go b/matcher_regexp.go index d3cdd93..bc0a4d4 100644 --- a/matcher_regexp.go +++ b/matcher_regexp.go @@ -5,7 +5,7 @@ import ( "strings" "github.com/hashicorp/golang-lru" - "github.com/ory-am/common/compiler" + "github.com/ory/ladon/compiler" "github.com/pkg/errors" )