Skip to content

Commit

Permalink
proxy: Make subject configurable using go template (#129)
Browse files Browse the repository at this point in the history
Signed-off-by: Lars Sjöström <lars@radicore.se>
  • Loading branch information
lsjostro authored and aeneasr committed Oct 23, 2018
1 parent e4d0e26 commit ee9dcdd
Show file tree
Hide file tree
Showing 22 changed files with 77 additions and 41 deletions.
3 changes: 1 addition & 2 deletions cmd/helper_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ import (
"crypto/tls"
"encoding/base64"
"fmt"
"net/url"
"strings"
"time"

"net/url"

"github.com/ory/fosite"
"github.com/ory/go-convenience/stringsx"
"github.com/ory/hydra/sdk/go/hydra"
Expand Down
1 change: 0 additions & 1 deletion cmd/migrate_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"fmt"

"os"

"github.com/ory/oathkeeper/rule"
Expand Down
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package cmd
import (
"fmt"
"os"

"time"

"github.com/sirupsen/logrus"
Expand Down
1 change: 0 additions & 1 deletion cmd/rules_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package cmd

import (
"fmt"

"net/http"

"github.com/ory/oathkeeper/sdk/go/oathkeeper"
Expand Down
3 changes: 1 addition & 2 deletions cmd/rules_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
package cmd

import (
"net/http"

"fmt"
"net/http"

"github.com/ory/oathkeeper/sdk/go/oathkeeper"
"github.com/spf13/cobra"
Expand Down
3 changes: 1 addition & 2 deletions cmd/rules_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"

"bytes"

"github.com/ory/oathkeeper/rule"
"github.com/ory/oathkeeper/sdk/go/oathkeeper"
"github.com/ory/oathkeeper/sdk/go/oathkeeper/swagger"
Expand Down
3 changes: 1 addition & 2 deletions cmd/rules_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
package cmd

import (
"net/http"

"fmt"
"net/http"

"github.com/ory/oathkeeper/pkg"
"github.com/ory/oathkeeper/sdk/go/oathkeeper"
Expand Down
1 change: 0 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package cmd

import (
"fmt"

"os"
"strconv"

Expand Down
3 changes: 1 addition & 2 deletions cmd/serve_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ import (
"net/http/httputil"

"github.com/meatballhat/negroni-logrus"
"github.com/ory/metrics-middleware"

"github.com/ory/go-convenience/corsx"
"github.com/ory/graceful"
"github.com/ory/keto/sdk/go/keto"
"github.com/ory/metrics-middleware"
"github.com/ory/oathkeeper/proxy"
"github.com/ory/oathkeeper/rule"
"github.com/ory/oathkeeper/sdk/go/oathkeeper"
Expand Down
6 changes: 2 additions & 4 deletions proxy/authenticator_jwt.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package proxy

import (
"encoding/json"
"net/http"

"bytes"
"crypto/ecdsa"
"crypto/rsa"
"encoding/json"
"fmt"

"net/http"
"net/url"

"github.com/dgrijalva/jwt-go"
Expand Down
1 change: 0 additions & 1 deletion proxy/authenticator_oauth2_client_credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"net/http"

"net/url"

"github.com/ory/oathkeeper/helper"
Expand Down
3 changes: 1 addition & 2 deletions proxy/authenticator_oauth2_introspection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package proxy

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"

"context"
"net/url"
"strings"

Expand Down
3 changes: 1 addition & 2 deletions proxy/authenticator_oauth2_introspection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import (
"encoding/json"
"fmt"
"net/http"
"testing"

"net/http/httptest"
"testing"

"github.com/julienschmidt/httprouter"
"github.com/ory/fosite"
Expand Down
38 changes: 37 additions & 1 deletion proxy/authorizer_keto_warden.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ package proxy
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"text/template"
"time"

"github.com/asaskevich/govalidator"
Expand All @@ -38,6 +40,7 @@ import (
type AuthorizerKetoWardenConfiguration struct {
RequiredAction string `json:"required_action" valid:",required"`
RequiredResource string `json:"required_resource" valid:",required"`
Subject string `json:"subject"`
}

type AuthorizerKetoWarden struct {
Expand Down Expand Up @@ -89,11 +92,20 @@ func (a *AuthorizerKetoWarden) Authorize(r *http.Request, session *Authenticatio
return errors.WithStack(err)
}

subject := session.Subject
if cf.Subject != "" {
templateId := fmt.Sprintf("%s:%s", rl.ID, "subject")
subject, err = a.ParseSubject(session, templateId, cf.Subject)
if err != nil {
return errors.WithStack(err)
}
}

defaultSession, response, err := a.K.IsSubjectAuthorized(swagger.WardenSubjectAuthorizationRequest{
Action: compiled.ReplaceAllString(r.URL.String(), cf.RequiredAction),
Resource: compiled.ReplaceAllString(r.URL.String(), cf.RequiredResource),
Context: a.contextCreator(r),
Subject: session.Subject,
Subject: subject,
})
if err != nil {
return errors.WithStack(err)
Expand All @@ -110,3 +122,27 @@ func (a *AuthorizerKetoWarden) Authorize(r *http.Request, session *Authenticatio

return nil
}
func (a *AuthorizerKetoWarden) ParseSubject(session *AuthenticationSession, templateId, templateString string) (string, error) {
tmplFn := template.New("rules").
Option("missingkey=zero").
Funcs(template.FuncMap{
"print": func(i interface{}) string {
if i == nil {
return ""
}
return fmt.Sprintf("%v", i)
},
})

tmpl, err := tmplFn.New(templateId).Parse(templateString)
if err != nil {
return "", err
}

subject := bytes.Buffer{}
err = tmpl.Execute(&subject, session)
if err != nil {
return "", err
}
return subject.String(), nil
}
24 changes: 24 additions & 0 deletions proxy/authorizer_keto_warden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ func TestAuthorizerKetoWarden(t *testing.T) {
session: &AuthenticationSession{Subject: "peter"},
expectErr: false,
},
{
config: []byte(`{ "required_action": "action:$1:$2", "required_resource": "resource:$1:$2", "subject": "{{ .Extra.name }}" }`),
rule: &rule.Rule{
Match: rule.RuleMatch{
Methods: []string{"POST"},
URL: "https://localhost/api/users/<[0-9]+>/<[a-z]+>",
},
},
r: &http.Request{URL: mustParseURL(t, "https://localhost/api/users/1234/abcde")},
setup: func(t *testing.T, m *MockWardenSDK) {
m.EXPECT().IsSubjectAuthorized(gomock.Eq(swagger.WardenSubjectAuthorizationRequest{
Action: "action:1234:abcde",
Resource: "resource:1234:abcde",
Context: map[string]interface{}{},
Subject: "peter",
})).Return(
&swagger.WardenSubjectAuthorizationResponse{Allowed: true},
&swagger.APIResponse{Response: &http.Response{StatusCode: http.StatusOK}},
nil,
)
},
session: &AuthenticationSession{Extra: map[string]interface{}{"name": "peter"}},
expectErr: false,
},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
c := gomock.NewController(t)
Expand Down
3 changes: 1 addition & 2 deletions proxy/credentials_issuer_id_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ package proxy

import (
"encoding/json"
"fmt"
"net/http"
"strings"
"testing"
"time"

"fmt"

"github.com/dgrijalva/jwt-go"
"github.com/go-errors/errors"
"github.com/ory/oathkeeper/rsakey"
Expand Down
3 changes: 1 addition & 2 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ import (
"context"
"io/ioutil"
"net/http"
"strings"

"net/url"
"strings"

"github.com/ory/herodot"
"github.com/ory/oathkeeper/rsakey"
Expand Down
6 changes: 2 additions & 4 deletions proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ import (
"net/http"
"net/http/httptest"
"net/http/httputil"
"testing"

"strings"

"net/url"
"strconv"
"strings"
"testing"

"github.com/ory/oathkeeper/rule"
"github.com/stretchr/testify/assert"
Expand Down
4 changes: 1 addition & 3 deletions rsakey/manager_hydra.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ package rsakey

import (
"crypto/rsa"
"net/http"

"encoding/json"

"net/http"
"strings"

"github.com/ory/hydra/sdk/go/hydra"
Expand Down
4 changes: 1 addition & 3 deletions rsakey/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,13 @@
package rsakey

import (
"crypto/rsa"
"log"
"net/http"
"os"
"testing"

"time"

"crypto/rsa"

"github.com/ory/dockertest"
"github.com/ory/hydra/sdk/go/hydra"
"github.com/pkg/errors"
Expand Down
1 change: 0 additions & 1 deletion rule/matcher_cached.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package rule

import (
"net/url"

"sync"

"github.com/ory/oathkeeper/helper"
Expand Down
3 changes: 1 addition & 2 deletions rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@
package rule

import (
"strings"

"encoding/json"
"hash/crc32"
"net/url"
"regexp"
"strings"

"github.com/ory/ladon/compiler"
"github.com/pkg/errors"
Expand Down

0 comments on commit ee9dcdd

Please sign in to comment.