Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support jwt plugin #18

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion pkg/kong/consumer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kong

import (
"fmt"

v1 "github.com/apache/apisix-ingress-controller/pkg/types/apisix/v1"
"github.com/api7/kong-to-apisix/pkg/utils"
)
Expand All @@ -19,12 +21,30 @@ func MigrateConsumer(kongConfig *KongConfig, configYamlAll *[]utils.YamlItem) (*
}

// TODO: need to test then it got multiple key
if c.KeyAuthCredentials[0].Key != "" {
if c.KeyAuthCredentials != nil {
pluginConfig := make(map[string]interface{})
pluginConfig["key"] = c.KeyAuthCredentials[0].Key

apisixConsumer.Plugins = v1.Plugins{"key-auth": pluginConfig}
}
if c.JWTCredentials != nil {
cred := c.JWTCredentials[0]
pluginConfig := make(map[string]interface{})
pluginConfig["key"] = cred.Key
pluginConfig["secret"] = cred.Secret
algorithm := cred.Algorithm
if algorithm == "HS384" || algorithm == "ES256" {
fmt.Printf("APISIX JWT have not support %s yet\n", algorithm)
} else {
pluginConfig["algorithm"] = algorithm
if cred.Secret != "" {
pluginConfig["secret"] = cred.Secret
}
if cred.RSA_Public_Key != "" {
pluginConfig["rsa_public_key"] = cred.RSA_Public_Key
}
}
}
apisixConsumers = append(apisixConsumers, *apisixConsumer)
}

Expand Down
12 changes: 9 additions & 3 deletions pkg/kong/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var pluginMap = map[string]func(p Plugin) (v1.Plugins, []utils.YamlItem, error){
"proxy-cache": proxyCache,
"key-auth": keyAuth,
"rate-limiting": rateLimiting,
"jwt": jwt,
}

// TODO: some configuration need to be configured in config.yaml
Expand Down Expand Up @@ -77,10 +78,15 @@ func rateLimiting(p Plugin) (v1.Plugins, []utils.YamlItem, error) {
}

func keyAuth(p Plugin) (v1.Plugins, []utils.YamlItem, error) {
pluginConfig := make(map[string]interface{})
pluginConfig["key"] = p.Config["key_names"]

emptyMap := make(map[string]interface{})

return v1.Plugins{"key-auth": emptyMap}, nil, nil
}

func jwt(p Plugin) (v1.Plugins, []utils.YamlItem, error) {
pluginConfig := make(map[string]interface{})
pluginConfig["base64_secret"] = p.Config["secret_is_base64"]
pluginConfig["exp"] = p.Config["maximum_expiration"]

return v1.Plugins{"jwt": pluginConfig}, nil, nil
}
6 changes: 6 additions & 0 deletions pkg/kong/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ type Consumers []struct {
KeyAuthCredentials []struct {
Key string `yaml:"key"`
} `yaml:"keyauth_credentials,omitempty"`
JWTCredentials []struct {
Key string `yaml:"key"`
Secret string `yaml:"secret"`
Algorithm string `yaml:"algorithm"`
RSA_Public_Key string `yaml:"rsa_public_key"`
} `yaml:"jwt_credentials,omitempty"`
}

type Plugins []Plugin
Expand Down