Skip to content

Commit

Permalink
feat: bootstrap command line
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelgautier committed Mar 19, 2023
1 parent ff12653 commit aa694df
Show file tree
Hide file tree
Showing 9 changed files with 4,847 additions and 0 deletions.
65 changes: 65 additions & 0 deletions cmd/generate/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package generate

import (
"context"
"fmt"
"net/url"
"path/filepath"
"runtime"

"github.com/cerberauth/openapi-oathkeeper/generator"
"github.com/getkin/kin-openapi/openapi3"
"github.com/spf13/cobra"
)

var (
_, b, _, _ = runtime.Caller(0)
basepath = filepath.Dir(b)
swaggerFilepath string
swaggerUrl string
outputPath string
)

func NewGenerateCmd() (generateCmd *cobra.Command) {
generateCmd = &cobra.Command{
Use: "generate",
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()

var doc *openapi3.T
var err error

if swaggerUrl != "" {
uri, urlerr := url.Parse(swaggerUrl)
if urlerr != nil {
fmt.Print(urlerr)
return
}

doc, err = openapi3.NewLoader().LoadFromURI(uri)
}

if swaggerFilepath != "" {
doc, err = openapi3.NewLoader().LoadFromFile(swaggerFilepath)
}

if err != nil {
fmt.Print(err)
return
}

rules, err := generator.New().Document(doc).Generate(ctx)
if err != nil {
fmt.Print(err)
return
}

fmt.Print(string(rules))
},
}
generateCmd.PersistentFlags().StringVarP(&swaggerUrl, "url", "u", "", "OpenAPI URL")
generateCmd.PersistentFlags().StringVarP(&swaggerFilepath, "file", "f", "", "OpenAPI File Path")
generateCmd.PersistentFlags().StringVarP(&outputPath, "output", "o", ".", "OAthKeeper Rules output path")

return generateCmd
}
29 changes: 29 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cmd

import (
"os"

"github.com/spf13/cobra"

"github.com/cerberauth/openapi-oathkeeper/cmd/generate"
)

func NewRootCmd() (cmd *cobra.Command) {
var rootCmd = &cobra.Command{
Use: "openapi-oathkeeper",
Short: "Generate Ory OAthKeeper Rules from OpenAPI 3.0 or Swagger",
}
rootCmd.AddCommand(generate.NewGenerateCmd())

return rootCmd
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the RootCmd.
func Execute() {
c := NewRootCmd()

if err := c.Execute(); err != nil {
os.Exit(1)
}
}
45 changes: 45 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package generator

import (
"context"
"encoding/json"

"github.com/getkin/kin-openapi/openapi3"
"github.com/ory/oathkeeper/rule"
)

type Generator struct {
doc *openapi3.T
}

func New() *Generator {
return &Generator{}
}

func (g *Generator) Document(d *openapi3.T) *Generator {
g.doc = d

return g
}

func (g *Generator) Generate(ctx context.Context) ([]byte, error) {
err := g.doc.Validate(ctx)
if err != nil {
return nil, err
}

rules := []rule.Rule{}
// for _, p := range g.doc.Paths {
// for _, o := range p.Operations() {
// basePath, _ := o.Servers.BasePath()
// matchUrl, _ := url.JoinPath(basePath, o.ExternalDocs.URL)
// rules = append(rules, rule.Rule{
// ID: o.OperationID,
// Description: o.Description,
// Match: &rule.Match{URL: matchUrl},
// })
// }
// }

return json.Marshal(rules)
}
28 changes: 28 additions & 0 deletions generator/generator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package generator

import (
"context"
"path"
"path/filepath"
"runtime"
"testing"

"github.com/bmizerany/assert"
"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/require"
)

var (
_, b, _, _ = runtime.Caller(0)
basepath = filepath.Dir(b)
)

func TestGenerateFromOpenAPI(t *testing.T) {
doc, err := openapi3.NewLoader().LoadFromFile(path.Join(basepath, "../test/stub/petstore.openapi.json"))
require.NoError(t, err)

ctx := context.Background()
rules, err := New().Document(doc).Generate(ctx)
require.NoError(t, err)
assert.Equal(t, string(rules), "[]")
}
171 changes: 171 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
module github.com/cerberauth/openapi-oathkeeper

go 1.19

require (
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869
github.com/getkin/kin-openapi v0.114.0
github.com/ory/oathkeeper v0.40.1
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
)

require (
cloud.google.com/go v0.102.0 // indirect
cloud.google.com/go/compute v1.7.0 // indirect
cloud.google.com/go/iam v0.3.0 // indirect
cloud.google.com/go/storage v1.22.1 // indirect
github.com/Azure/azure-pipeline-go v0.2.2 // indirect
github.com/Azure/azure-storage-blob-go v0.9.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/avast/retry-go/v4 v4.3.0 // indirect
github.com/aws/aws-sdk-go v1.34.28 // indirect
github.com/blang/semver v3.5.1+incompatible // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/dlclark/regexp2 v1.2.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/gobuffalo/pop/v6 v6.0.8 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-yaml v1.9.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.0.0 // indirect
github.com/golang/gddo v0.0.0-20190904175337-72a348e765d2 // indirect
github.com/golang/glog v1.0.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-replayers/httpreplay v1.1.1 // indirect
github.com/google/martian/v3 v3.3.3-0.20220816151257-0f7e6797a04d // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/wire v0.4.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa // indirect
github.com/googleapis/gax-go v2.0.2+incompatible // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/googleapis/go-type-adapters v1.0.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.12.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.2.0 // indirect
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf // indirect
github.com/invopop/yaml v0.1.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/jackc/pgx/v4 v4.17.2 // indirect
github.com/jandelgado/gcov2lcov v1.0.5 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmoiron/sqlx v1.3.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/knadh/koanf v1.4.4 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-ieproxy v0.0.1 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/mattn/goveralls v0.0.6 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/nyaruka/phonenumbers v1.1.1 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/ory/fosite v0.36.1 // indirect
github.com/ory/go-acc v0.2.8 // indirect
github.com/ory/go-convenience v0.1.0 // indirect
github.com/ory/gojsonreference v0.0.0-20190720135523-6b606c2d8ee8 // indirect
github.com/ory/gojsonschema v1.2.0 // indirect
github.com/ory/herodot v0.9.13 // indirect
github.com/ory/jsonschema/v3 v3.0.7 // indirect
github.com/ory/ladon v1.1.0 // indirect
github.com/ory/viper v1.7.5 // indirect
github.com/ory/x v0.0.532 // indirect
github.com/pborman/uuid v1.2.1 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/perimeterx/marshmallow v1.1.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/seatgeek/logrus-gelf-formatter v0.0.0-20210414080842-5b05eb8ff761 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/tidwall/gjson v1.14.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.36.4 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.36.4 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.11.1 // indirect
go.opentelemetry.io/contrib/propagators/jaeger v1.11.1 // indirect
go.opentelemetry.io/contrib/samplers/jaegerremote v0.5.2 // indirect
go.opentelemetry.io/otel v1.11.1 // indirect
go.opentelemetry.io/otel/exporters/jaeger v1.11.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.11.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.9.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.9.0 // indirect
go.opentelemetry.io/otel/exporters/zipkin v1.11.1 // indirect
go.opentelemetry.io/otel/metric v0.33.0 // indirect
go.opentelemetry.io/otel/sdk v1.11.1 // indirect
go.opentelemetry.io/otel/trace v1.11.1 // indirect
go.opentelemetry.io/proto/otlp v0.18.0 // indirect
gocloud.dev v0.20.0 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/tools v0.5.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.84.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20221025140454-527a21cfbd71 // indirect
google.golang.org/grpc v1.50.1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit aa694df

Please sign in to comment.