-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add OWNERS * Add OWNERS * Create base jwt validation
- Loading branch information
Jakub Błaszczyk
authored
Aug 27, 2019
1 parent
f406c28
commit 02f4bb1
Showing
10 changed files
with
308 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package v2alpha1 | ||
|
||
import "k8s.io/apimachinery/pkg/runtime" | ||
|
||
// JWTModeConfig config for JWT mode | ||
type JWTModeConfig struct { | ||
Issuer string `json:"issuer"` | ||
JWKS []string `json:"jwks,omitempty"` | ||
Mode InternalConfig `json:"mode"` | ||
} | ||
|
||
// InternalConfig internal config, specific for JWT modes | ||
type InternalConfig struct { | ||
Name string `json:"name"` | ||
Config *runtime.RawExtension `json:"config,omitempty"` | ||
} | ||
|
||
// JWTModeALL representation of config for the ALL mode | ||
type JWTModeALL struct { | ||
Scopes []string `json:"scopes"` | ||
} | ||
|
||
// JWTModeInclude representation of config for the INCLUDE mode | ||
type JWTModeInclude struct { | ||
Paths []IncludePath `json:"paths"` | ||
} | ||
|
||
// IncludePath Path for INCLUDE mode | ||
type IncludePath struct { | ||
Path string `json:"path"` | ||
Scopes []string `json:"scopes"` | ||
Methods []string `json:"methods"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,56 @@ | ||
package validation | ||
|
||
import "k8s.io/apimachinery/pkg/runtime" | ||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
|
||
gatewayv2alpha1 "github.com/kyma-incubator/api-gateway/api/v2alpha1" | ||
"github.com/pkg/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
var ( | ||
jwtModes = []string{"ALL", "INCLUDE", "EXCLUDE"} | ||
) | ||
|
||
type jwt struct{} | ||
|
||
func (j *jwt) Validate(config *runtime.RawExtension) error { | ||
var template gatewayv2alpha1.JWTModeConfig | ||
|
||
if !configNotEmpty(config) { | ||
return fmt.Errorf("supplied config cannot be empty") | ||
} | ||
err := json.Unmarshal(config.Raw, &template) | ||
if err != nil { | ||
return errors.WithStack(err) | ||
} | ||
if !j.isValidURL(template.Issuer) { | ||
return fmt.Errorf("issuer field is empty or not a valid url") | ||
} | ||
if !j.isValidMode(template.Mode.Name) { | ||
return fmt.Errorf("supplied mode is invalid: %v, valid modes are: ALL, INCLUDE, EXCLUDE", template.Mode.Name) | ||
} | ||
return nil | ||
} | ||
|
||
func (j *jwt) isValidURL(toTest string) bool { | ||
if len(toTest) == 0 { | ||
return false | ||
} | ||
_, err := url.ParseRequestURI(toTest) | ||
if err != nil { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func (j *jwt) isValidMode(mode string) bool { | ||
for _, b := range jwtModes { | ||
if b == mode { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package validation_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ghodss/yaml" | ||
gatewayv2alpha1 "github.com/kyma-incubator/api-gateway/api/v2alpha1" | ||
"github.com/kyma-incubator/api-gateway/internal/validation" | ||
"gotest.tools/assert" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" | ||
) | ||
|
||
var ( | ||
validYamlForJWT = ` | ||
issuer: http://dex.kyma.local | ||
jwks: ["a", "b"] | ||
mode: | ||
name: ALL | ||
config: | ||
scopes: ["foo", "bar"] | ||
` | ||
invalidIssuer = ` | ||
issuer: this-is-not-an-url | ||
` | ||
invalidJWTMode = ` | ||
issuer: http://dex.kyma.local | ||
jwks: ["a", "b"] | ||
mode: | ||
name: CLASSIFIED_MODE_DONT_USE | ||
config: | ||
top: secret | ||
` | ||
logJWT = logf.Log.WithName("jwt-validate-test") | ||
) | ||
|
||
func TestJWTValidate(t *testing.T) { | ||
strategy, err := validation.NewFactory(logJWT).StrategyFor(gatewayv2alpha1.JWT) | ||
assert.NilError(t, err) | ||
|
||
jsonData, err := yaml.YAMLToJSON([]byte(invalidIssuer)) | ||
assert.NilError(t, err) | ||
assert.Error(t, strategy.Validate(&runtime.RawExtension{Raw: jsonData}), "issuer field is empty or not a valid url") | ||
|
||
jsonData, err = yaml.YAMLToJSON([]byte(invalidJWTMode)) | ||
assert.NilError(t, err) | ||
assert.Error(t, strategy.Validate(&runtime.RawExtension{Raw: jsonData}), "supplied mode is invalid: CLASSIFIED_MODE_DONT_USE, valid modes are: ALL, INCLUDE, EXCLUDE") | ||
|
||
jsonData, err = yaml.YAMLToJSON([]byte(validYamlForJWT)) | ||
assert.NilError(t, err) | ||
assert.NilError(t, strategy.Validate(&runtime.RawExtension{Raw: jsonData})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters