Skip to content

Inc 456 #146

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

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
f0a2334
feat(tls) add client certificate support when TLS handshaking with
dndx Mar 30, 2020
4656a69
fix(init) make tests green and pass correct options to luasec_opts
Jan 14, 2021
381f302
add support for scram-sha-256 with channel binding
Dec 1, 2020
b1b322b
release: 2.2.0
Jan 15, 2021
cd9f092
fix(init) use lua-resty-openssl instead of luaossl to let it run
fffonion Feb 14, 2022
efb25cd
feat(socket) change LuaSec ssl_protocol default options
Feb 26, 2021
96fc9eb
rebuild moon, fix discrepancy in lua/moon #103
fffonion Mar 3, 2021
99b0190
release: 2.2.0
fffonion Feb 18, 2022
f112e16
feat(tls) add client certificate support when TLS handshaking with
dndx Mar 30, 2020
9c821d1
fix(init) make tests green and pass correct options to luasec_opts
Jan 14, 2021
ca7097f
add support for scram-sha-256 with channel binding
Dec 1, 2020
dec9dd3
release: 2.2.0
Jan 15, 2021
0ad053e
fix(init) use lua-resty-openssl instead of luaossl to let it run
fffonion Feb 14, 2022
2f2b028
feat(socket) change LuaSec ssl_protocol default options
Feb 26, 2021
3cbe7be
rebuild moon, fix discrepancy in lua/moon #103
fffonion Mar 3, 2021
d214840
release: 2.2.1
fffonion Feb 18, 2022
2d35aec
Merge branch 'kong-dev' into chore/rebase_1_14_0
tyler-ball Feb 25, 2022
1d695ef
I don't know moonscript
tyler-ball Feb 25, 2022
b16a65f
Missed a config API update
tyler-ball Feb 25, 2022
6f465cd
Remove duplicated function definition
gruceo May 23, 2022
a9d69dd
Merge pull request #18 from Kong/chore/rebase_1_14_0
gruceo May 23, 2022
d9c13cb
release: 2.2.2
gruceo May 23, 2022
3364340
chore(init) use newer API (setclientcert)
StarlightIbuki May 31, 2022
4c50101
fix(init) back compability
StarlightIbuki May 31, 2022
7a90c4f
fix(init) set cert does not call handshake
StarlightIbuki May 31, 2022
35c9cde
fix(init) apply suggestions
StarlightIbuki Jun 1, 2022
ce61af9
use setclientcert instead of tlshandshake
StarlightIbuki Jun 7, 2022
54f7c72
fix(pgmoon) set the correct signature digest name
outsinre Jun 23, 2022
400465c
release: 2.2.3
outsinre Jun 27, 2022
917ad45
Merge branch 'master' into kong-dev
gruceo Jun 29, 2022
e388af5
fix(ssl) if tlshandshake does not exist, fallback to sslhandshake
gruceo Jun 30, 2022
79b2c0a
release: 2.3.0
gruceo Jun 30, 2022
67dc431
fix(ssl) init.moon tlshandshake -> sslhandshake fallback
gruceo Aug 8, 2022
0aab517
Merge pull request #22 from Kong/kong-dev-sync-with-master
tyler-ball Aug 10, 2022
077350c
fix(init) fix boringssl digest mapping
fffonion Nov 30, 2022
cb81d8b
release: 2.3.1, add kong-prefix to rockspec name
gszr Dec 1, 2022
dfffea2
fix the typo which causes mtls error "bad client cert type"
catbro666 Jan 17, 2023
9802378
Merge pull request #28 from Kong/FTI-4678-mtls-bad-client-cert-type
VicYP Jan 18, 2023
469f259
release: 2.3.2.0 (kong internal version)
gszr Jan 26, 2023
d43ec53
Merge pull request #29 from Kong/release/2.3.2.0
catbro666 Jan 30, 2023
7979d97
feat(ci/KAG-1807): add lint and sast workflows using shared actions (…
saisatishkarra Aug 1, 2023
f2940f2
chore(ci): pin 3rd-party actions to specific commit hashes (#35)
Water-Melon Jan 16, 2025
92c99b7
Updated sha for tj-actions
rpawar16 Mar 15, 2025
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
44 changes: 44 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Lint

on:
pull_request: {}
workflow_dispatch: {}
push:
branches:
- main
- kong-dev
- master

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
lua-check:
name: Lua Check
runs-on: ubuntu-latest
permissions:
contents: read
issues: read
checks: write
pull-requests: write
if: (github.actor != 'dependabot[bot]')

steps:
- name: Checkout source code
uses: actions/checkout@v3

# Optional step to run on only changed files
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@4edd678ac3f81e2dc578756871e4d00c19191daf
with:
files: |
**.lua

- name: Lua Check
if: steps.changed-files.outputs.any_changed == 'true'
uses: Kong/public-shared-actions/code-check-actions/lua-lint@33449c46c6766a3d3c8f167cc383381225862b36
with:
additional_args: '--no-default-config'
files: ${{ steps.changed-files.outputs.all_changed_files }}
28 changes: 28 additions & 0 deletions .github/workflows/sast.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: SAST

on:
pull_request: {}
push:
branches:
- master
- main
- kong-dev
workflow_dispatch: {}


jobs:
semgrep:
name: Semgrep SAST
runs-on: ubuntu-latest
permissions:
# required for all workflows
security-events: write
# only required for workflows in private repositories
actions: read
contents: read

if: (github.actor != 'dependabot[bot]')

steps:
- uses: actions/checkout@v3
- uses: Kong/public-shared-actions/security-actions/semgrep@33449c46c6766a3d3c8f167cc383381225862b36
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ jobs:

steps:
- uses: actions/checkout@master
- uses: leafo/gh-actions-lua@master
- uses: leafo/gh-actions-lua@ef9239997afff6d2d8b42ec9729c1d4b7f2f1302
with:
luaVersion: ${{ matrix.luaVersion }}

- uses: leafo/gh-actions-luarocks@master
- uses: leafo/gh-actions-luarocks@e65774a6386cb4f24e293dca7fc4ff89165b64c5

- name: dependencies
run: |
Expand Down Expand Up @@ -64,11 +64,11 @@ jobs:
steps:
- uses: actions/checkout@master

- uses: leafo/gh-actions-openresty@main
- uses: leafo/gh-actions-openresty@4bd905d98dcbee29a2d95771d0b1e743e890fdfa
with:
openrestyVersion: "1.19.9.1"

- uses: leafo/gh-actions-luarocks@master
- uses: leafo/gh-actions-luarocks@e65774a6386cb4f24e293dca7fc4ff89165b64c5
with:
withLuaPath: "/usr/local/openresty/luajit/"

Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
of the [cosocket
api](https://github.com/openresty/lua-nginx-module#ngxsockettcp) to provide
asynchronous queries but it also works in the regular any Lua environment where
[LuaSocket][] or [cqueues][] is available.
[LuaSocket][] or [cqueues][] is available. pgmoon optionally requires:

- [LuaCrypto][2] for authentication with MD5
- [LuaSec][6] for SSL connections

For authentication with SCRAM-SHA-256 (without channel binding support) pgmoon requires [luaossl](https://luarocks.org/modules/daurnimator/luaossl) only. For authentication with SCRAM-SHA-256-PLUS (with channel binding support) it requires [LuaSec][6] for the socket type `luasocket` and [lua-resty-openssl](https://luarocks.org/modules/fffonion/lua-resty-openssl) for the socket type `nginx`.

It's a perfect candidate for running your queries both inside OpenResty's
environment and on the command line (eg. tests) in web frameworks like [Lapis][].
Expand Down
11 changes: 6 additions & 5 deletions pgmoon-dev-1.rockspec → kong-pgmoon-2.3.2.0-1.rockspec
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package = "pgmoon"
version = "dev-1"
package = "kong-pgmoon"
version = "2.3.2.0-1"

source = {
url = "git+https://github.com/leafo/pgmoon.git"
url = "git+https://github.com/Kong/pgmoon.git",
tag = "2.3.2.0" -- internal kong version adds an extra digit to the version
}

description = {
summary = "Postgres driver for OpenResty and Lua",
detailed = [[PostgreSQL driver written in pure Lua for use with OpenResty's cosocket API. Can also be used in regular Lua with LuaSocket and LuaCrypto.]],
homepage = "https://github.com/leafo/pgmoon",
maintainer = "Leaf Corcoran <[email protected]>",
homepage = "https://github.com/Kong/pgmoon",
maintainer = "Kong Inc",
license = "MIT"
}

Expand Down
53 changes: 45 additions & 8 deletions pgmoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ do
local _obj_0 = require("pgmoon.bit")
rshift, lshift, band, bxor = _obj_0.rshift, _obj_0.lshift, _obj_0.band, _obj_0.bxor
end
local pl_file
local ssl
if ngx then
pl_file = require("pl.file")
ssl = require("ngx.ssl")
end
local unpack = table.unpack or unpack
local DEBUG = false
local VERSION = "1.15.0"
local VERSION = "2.3.2.0"
local _len
_len = function(thing, t)
if t == nil then
Expand Down Expand Up @@ -288,7 +294,7 @@ do
end,
create_cqueues_openssl_context = function(self)
if not (self.config.ssl_verify ~= nil or self.config.cert or self.config.key or self.config.ssl_version) then
return
return
end
local ssl_context = require("openssl.ssl.context")
local out = ssl_context.new(self.config.ssl_version)
Expand All @@ -307,12 +313,20 @@ do
return out
end,
create_luasec_opts = function(self)
local key = self.config.key
local cert = self.config.cert
if self.sock_type == "nginx" and key and cert then
key = assert(ssl.parse_pem_priv_key(pl_file.read(key, true)))
cert = assert(ssl.parse_pem_cert(pl_file.read(cert, true)))
end
return {
key = self.config.key,
certificate = self.config.cert,
key = key,
certificate = cert,
cafile = self.config.cafile,
protocol = self.config.ssl_version,
verify = self.config.ssl_verify and "peer" or "none"
verify = self.config.ssl_verify and "peer" or "none",
ssl_version = self.config.ssl_version or "any",
options = { "all", "no_sslv2", "no_sslv3", "no_tlsv1" }
}
end,
auth = function(self)
Expand Down Expand Up @@ -401,9 +415,13 @@ do
local server_cert = self.sock:getpeercertificate()
pem, signature = server_cert:pem(), server_cert:getsignaturename()
end
signature = signature:lower()
if signature:match("^md5") or signature:match("^sha1") then
if signature:match("^md5") or signature:match("^sha1") or signature:match("sha1$") or signature:match("sha256$") then
signature = "sha256"
else
local objects = require("resty.openssl.objects")
local sigid = assert(objects.txt2nid(signature))
local digest_nid = assert(objects.find_sigid_algs(sigid))
signature = assert(objects.nid2table(digest_nid).sn)
end
cbind_data = assert(x509_digest(pem, signature))
end
Expand Down Expand Up @@ -936,7 +954,26 @@ do
if t == MSG_TYPE_B.parameter_status then
local _exp_0 = self.sock_type
if "nginx" == _exp_0 then
return self.sock:sslhandshake(false, nil, self.config.ssl_verify)
local luasec_opts = self.config.luasec_opts or self:create_luasec_opts()

-- version compability check to see if setclientcert is supported
if self.sock.setclientcert then
local ok, err_internal = self.sock:setclientcert(luasec_opts.certificate, luasec_opts.key)
if not ok then
return false, err_internal
end
return self.sock:sslhandshake(false, nil, self.config.ssl_verify)
else
if self.sock.tlshandshake then
return self.sock:tlshandshake({
verify = self.config.ssl_verify,
client_cert = luasec_opts.certificate,
client_priv_key = luasec_opts.key
})
else
return self.sock:sslhandshake(false, nil, self.config.ssl_verify)
end
end
elseif "luasocket" == _exp_0 then
return self.sock:sslhandshake(self.config.luasec_opts or self:create_luasec_opts())
elseif "cqueues" == _exp_0 then
Expand Down
44 changes: 39 additions & 5 deletions pgmoon/init.moon
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@ import insert from table

import rshift, lshift, band, bxor from require "pgmoon.bit"

local pl_file
local ssl

if ngx
pl_file = require "pl.file"
ssl = require "ngx.ssl"

local pl_file
local ssl

if ngx
pl_file = require "pl.file"
ssl = require "ngx.ssl"

unpack = table.unpack or unpack

-- Protocol documentation:
-- https://www.postgresql.org/docs/current/protocol-message-formats.html

DEBUG = false
VERSION = "1.15.0"
VERSION = "2.3.2.0"

_len = (thing, t=type(thing)) ->
switch t
Expand Down Expand Up @@ -306,12 +320,20 @@ class Postgres
out

create_luasec_opts: =>
key = @config.key
cert = @config.cert

if @sock_type == "nginx" and key and cert
key = assert(ssl.parse_pem_priv_key(pl_file.read(key, true)))
cert = assert(ssl.parse_pem_cert(pl_file.read(cert, true)))
{
key: @config.key
certificate: @config.cert
key: key
certificate: cert
cafile: @config.cafile
protocol: @config.ssl_version
verify: @config.ssl_verify and "peer" or "none"
verify: @config.ssl_verify and "peer" or "none",
ssl_version: @config.ssl_version or "any"
options: { "all", "no_sslv2", "no_sslv3", "no_tlsv1" }
}


Expand Down Expand Up @@ -912,7 +934,19 @@ class Postgres
if t == MSG_TYPE_B.parameter_status
switch @sock_type
when "nginx"
@sock\sslhandshake false, nil, @config.ssl_verify
luasec_opts = @config.luasec_opts or @create_luasec_opts!

-- version compability check to see if setclientcert is supported
if @sock.setclientcert
ok, err_internal = @sock\setclientcert luasec_opts.certificate, luasec_opts.key
if not ok
return false, err_internal
return @sock\sslhandshake false, nil, @config.ssl_verify
else
if @sock.tlshandshake
return @sock\tlshandshake { verify: @config.ssl_verify, client_cert: luasec_opts.certificate, client_priv_key: luasec_opts.key }
else
return @sock\sslhandshake false, nil, @config.ssl_verify
when "luasocket"
@sock\sslhandshake @config.luasec_opts or @create_luasec_opts!
when "cqueues"
Expand Down