-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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: add jwe decrypt plugin #10252
Merged
Merged
feat: add jwe decrypt plugin #10252
Changes from 7 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
6223178
feat: add jwe decrypt plugin
fishioon 88ca63b
feat: add unit test
fishioon 53167a1
feat: fix ci
fishioon be97b1b
feat: fix cr
fishioon 697aa4b
Merge branch 'master' of github.com:apache/apisix into feat-jwe
shreemaan-abhishek 96481ea
fix test error
shreemaan-abhishek 504bd7a
Merge branch 'master' of github.com:apache/apisix into feat-jwe
shreemaan-abhishek 22164eb
Update docs and add missing `secret` attribute
kayx23 e919e79
fix test name
shreemaan-abhishek e24587f
apply suggestions
shreemaan-abhishek 7ca6c57
use `is_base64_encoded`
shreemaan-abhishek fa3e66a
remove log
shreemaan-abhishek d21d9d7
two line breaks
shreemaan-abhishek e1e7e1d
Merge branch 'feat-jwe' of github.com:fishioon/apisix into feat-jwe
shreemaan-abhishek 1088887
use `resty.aes` instead of a `resty.openssl`
shreemaan-abhishek f370d41
use `dec`
shreemaan-abhishek a24e3fd
Apply suggestions from code review
shreemaan-abhishek b843ec4
fix test title
shreemaan-abhishek f2ab448
remove incorrect attributes from consumer conf in test
shreemaan-abhishek c2d889a
fix test title name
shreemaan-abhishek 098fa05
don't sleep
shreemaan-abhishek 1b43ae5
fix method usage
shreemaan-abhishek 6b20a33
add more tests
shreemaan-abhishek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
-- | ||
-- Licensed to the Apache Software Foundation (ASF) under one or more | ||
-- contributor license agreements. See the NOTICE file distributed with | ||
-- this work for additional information regarding copyright ownership. | ||
-- The ASF licenses this file to You under the Apache License, Version 2.0 | ||
-- (the "License"); you may not use this file except in compliance with | ||
-- the License. You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
-- | ||
local core = require("apisix.core") | ||
local consumer_mod = require("apisix.consumer") | ||
local base64 = require("ngx.base64") | ||
local plugin_name = "jwe-decrypt" | ||
local ngx = ngx | ||
local sub_str = string.sub | ||
local cipher = require("resty.openssl.cipher").new("aes-256-gcm") | ||
|
||
local schema = { | ||
type = "object", | ||
properties = { | ||
header = { | ||
type = "string", | ||
default = "Authorization" | ||
}, | ||
forward_header = { | ||
type = "string", | ||
default = "Authorization" | ||
}, | ||
strict = { | ||
type = "boolean", | ||
default = true | ||
} | ||
}, | ||
required = { "header", "forward_header" }, | ||
} | ||
|
||
local consumer_schema = { | ||
shreemaan-abhishek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type = "object", | ||
properties = { | ||
key = { type = "string" }, | ||
secret = { type = "string", minLength = 32 }, | ||
base64_secret = { type = "boolean" }, | ||
}, | ||
required = { "key", "secret" }, | ||
} | ||
|
||
|
||
local _M = { | ||
version = 0.1, | ||
priority = 2509, | ||
type = 'auth', | ||
name = plugin_name, | ||
schema = schema, | ||
consumer_schema = consumer_schema | ||
} | ||
|
||
|
||
function _M.check_schema(conf, schema_type) | ||
core.log.info("input conf: ", core.json.delay_encode(conf)) | ||
if schema_type == core.schema.TYPE_CONSUMER then | ||
return core.schema.check(consumer_schema, conf) | ||
end | ||
return core.schema.check(schema, conf) | ||
monkeyDluffy6017 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
local function get_secret(conf) | ||
local secret = conf.secret | ||
|
||
if conf.base64_secret then | ||
return base64.decode_base64(secret) | ||
end | ||
|
||
return secret | ||
end | ||
|
||
monkeyDluffy6017 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
local function load_jwe_token(jwe_token) | ||
local o = { valid = false } | ||
o.header, o.enckey, o.iv, o.ciphertext, o.tag = jwe_token:match("(.-)%.(.-)%.(.-)%.(.-)%.(.*)") | ||
if not o.header then | ||
return o | ||
end | ||
local he = base64.decode_base64url(o.header) | ||
if not he then | ||
return o | ||
end | ||
o.header_obj = core.json.decode(he) | ||
if not o.header_obj then | ||
return o | ||
end | ||
o.valid = true | ||
return o | ||
end | ||
|
||
|
||
local function jwe_decrypt_with_obj(o, consumer) | ||
local secret = get_secret(consumer.auth_conf) | ||
local dec = base64.decode_base64url | ||
return cipher:decrypt(secret, dec(o.iv), dec(o.ciphertext), false, o.header, dec(o.tag)) | ||
end | ||
|
||
|
||
local function jwe_encrypt(o, consumer) | ||
local secret = get_secret(consumer.auth_conf) | ||
local enc = base64.encode_base64url | ||
o.ciphertext = cipher:encrypt(secret, o.iv, o.plaintext, false, o.header) | ||
o.tag = cipher:get_aead_tag() | ||
return o.header .. ".." .. enc(o.iv) .. "." .. enc(o.ciphertext) .. "." .. enc(o.tag) | ||
end | ||
|
||
|
||
local function get_consumer(key) | ||
local consumer_conf = consumer_mod.plugin(plugin_name) | ||
if not consumer_conf then | ||
return nil | ||
end | ||
local consumers = consumer_mod.consumers_kv(plugin_name, consumer_conf, "key") | ||
if not consumers then | ||
return nil | ||
end | ||
core.log.info("consumers: ", core.json.delay_encode(consumers)) | ||
return consumers[key] | ||
end | ||
|
||
|
||
local function fetch_jwe_token(conf, ctx) | ||
local token = core.request.header(ctx, conf.header) | ||
if token then | ||
local prefix = sub_str(token, 1, 7) | ||
if prefix == 'Bearer ' or prefix == 'bearer ' then | ||
return sub_str(token, 8) | ||
end | ||
|
||
return token | ||
end | ||
end | ||
|
||
|
||
function _M.rewrite(conf, ctx) | ||
-- fetch token and hide credentials if necessary | ||
local jwe_token, err = fetch_jwe_token(conf, ctx) | ||
if not jwe_token and conf.strict then | ||
core.log.info("failed to fetch JWE token: ", err) | ||
return 403, { message = "missing JWE token in request" } | ||
end | ||
|
||
local jwe_obj = load_jwe_token(jwe_token) | ||
if not jwe_obj.valid then | ||
return 400, { message = "JWE token invalid" } | ||
end | ||
|
||
if not jwe_obj.header_obj.kid then | ||
return 400, { message = "missing kid in JWE token" } | ||
end | ||
|
||
local consumer = get_consumer(jwe_obj.header_obj.kid) | ||
if not consumer then | ||
return 400, { message = "invalid kid in JWE token" } | ||
end | ||
|
||
local plaintext, err = jwe_decrypt_with_obj(jwe_obj, consumer) | ||
if err ~= nil then | ||
return 400, { message = "failed to decrypt JWE token" } | ||
end | ||
core.request.set_header(ctx, conf.forward_header, plaintext) | ||
end | ||
|
||
local function gen_token() | ||
local args = core.request.get_uri_args() | ||
if not args or not args.key then | ||
return core.response.exit(400) | ||
end | ||
|
||
local key = args.key | ||
local payload = args.payload | ||
if payload then | ||
payload = ngx.unescape_uri(payload) | ||
end | ||
|
||
local consumer = get_consumer(key) | ||
if not consumer then | ||
return core.response.exit(404) | ||
end | ||
|
||
core.log.info("consumer: ", core.json.delay_encode(consumer)) | ||
|
||
local iv = args.iv | ||
if not iv then | ||
-- TODO: random bytes | ||
iv = "123456789012" | ||
end | ||
|
||
local obj = { | ||
iv = iv, | ||
plaintext = payload, | ||
header_obj = { | ||
kid = key, | ||
alg = "dir", | ||
enc = "A256GCM", | ||
}, | ||
} | ||
obj.header = base64.encode_base64url(core.json.encode(obj.header_obj)) | ||
local jwe_token = jwe_encrypt(obj, consumer) | ||
if jwe_token then | ||
return core.response.exit(200, jwe_token) | ||
end | ||
|
||
return core.response.exit(404) | ||
end | ||
|
||
|
||
function _M.api() | ||
return { | ||
{ | ||
methods = { "GET" }, | ||
uri = "/apisix/plugin/jwe/encrypt", | ||
handler = gen_token, | ||
} | ||
} | ||
end | ||
|
||
return _M |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use https://github.com/openresty/lua-resty-string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fishioon have you noticed this comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shreemaan-abhishek
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any issue in using the current method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we use
lua-resty-string
, we don't need to import any new library