-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
105301: pgcryptocipher: add helper function for parsing cipher method r=rafiss a=andyyang890 **pgcryptocipher: create new package for pgcrypto cipher functions** This patch creates a new package that will contain the implementation of pgcrypto cipher functions, along with related helpers. Release note: None ---- **pgcryptocipher: add helper function for parsing cipher method** This patch adds a helper function for parsing the cipher method string passed to pgcrypto cipher functions. Release note: None ---- Informs #21001 105559: testccl/sqlccl: unskip TestExplainRedactDDL r=mgartner a=michae2 `TestExplainRedactDDL` is a randomized SQL test which runs variants of `EXPLAIN (REDACT)` on random SQL statements and checks that an injected poison string is always redacted in the output. It is very similar to another randomized test, `TestExplainRedact`, but also includes DDL in the random statements. During development of v23.1 this test was skipped because the random DDL statements were running into other bugs unrelated to redaction. Now that things are more stable, let's unskip this test. Fixes: #99005 Epic: None Release note: None Co-authored-by: Andy Yang <[email protected]> Co-authored-by: Michael Erickson <[email protected]>
- Loading branch information
Showing
9 changed files
with
220 additions
and
35 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
This file was deleted.
Oops, something went wrong.
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,32 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "pgcryptocipher", | ||
srcs = [ | ||
"cipher_method.go", | ||
"doc.go", | ||
"padding.go", | ||
], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/sql/sem/builtins/pgcrypto/pgcryptocipher", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/sql/pgwire/pgcode", | ||
"//pkg/sql/pgwire/pgerror", | ||
"//pkg/util/errorutil/unimplemented", | ||
"@com_github_cockroachdb_errors//:errors", | ||
], | ||
) | ||
|
||
go_test( | ||
name = "pgcryptocipher_test", | ||
srcs = [ | ||
"cipher_method_test.go", | ||
"padding_test.go", | ||
], | ||
args = ["-test.timeout=295s"], | ||
embed = [":pgcryptocipher"], | ||
deps = [ | ||
"//pkg/util/leaktest", | ||
"@com_github_stretchr_testify//require", | ||
], | ||
) |
84 changes: 84 additions & 0 deletions
84
pkg/sql/sem/builtins/pgcrypto/pgcryptocipher/cipher_method.go
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,84 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package pgcryptocipher | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" | ||
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror" | ||
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented" | ||
) | ||
|
||
type cipherAlgorithm int | ||
|
||
const ( | ||
_ cipherAlgorithm = iota | ||
aesCipher | ||
) | ||
|
||
type cipherMode int | ||
|
||
const ( | ||
cbcMode cipherMode = iota | ||
) | ||
|
||
type cipherPadding int | ||
|
||
const ( | ||
pkcsPadding cipherPadding = iota | ||
noPadding | ||
) | ||
|
||
type cipherMethod struct { | ||
algorithm cipherAlgorithm | ||
mode cipherMode | ||
padding cipherPadding | ||
} | ||
|
||
func parseCipherMethod(s string) (cipherMethod, error) { | ||
cipherMethodRE := regexp.MustCompile("^(?P<algorithm>[[:alpha:]]+)(?:-(?P<mode>[[:alpha:]]+))?(?:/pad:(?P<padding>[[:alpha:]]+))?$") | ||
|
||
submatches := cipherMethodRE.FindStringSubmatch(s) | ||
if submatches == nil { | ||
return cipherMethod{}, pgerror.Newf(pgcode.InvalidParameterValue, `cipher method has wrong format: "%s"`, s) | ||
} | ||
|
||
ret := cipherMethod{} | ||
|
||
switch algorithm := submatches[cipherMethodRE.SubexpIndex("algorithm")]; strings.ToLower(algorithm) { | ||
case "aes": | ||
ret.algorithm = aesCipher | ||
case "bf": | ||
return cipherMethod{}, unimplemented.NewWithIssue(105466, "Blowfish is insecure and not supported") | ||
default: | ||
return cipherMethod{}, pgerror.Newf(pgcode.InvalidParameterValue, `cipher method has unsupported algorithm: "%s"`, algorithm) | ||
} | ||
|
||
switch mode := submatches[cipherMethodRE.SubexpIndex("mode")]; strings.ToLower(mode) { | ||
case "", "cbc": | ||
case "ecb": | ||
return cipherMethod{}, unimplemented.NewWithIssue(105466, "ECB mode is insecure and not supported") | ||
default: | ||
return cipherMethod{}, pgerror.Newf(pgcode.InvalidParameterValue, `cipher method has unsupported mode: "%s"`, mode) | ||
} | ||
|
||
switch padding := submatches[cipherMethodRE.SubexpIndex("padding")]; strings.ToLower(padding) { | ||
case "", "pkcs": | ||
case "none": | ||
ret.padding = noPadding | ||
default: | ||
return cipherMethod{}, pgerror.Newf(pgcode.InvalidParameterValue, `cipher method has unsupported padding: "%s"`, padding) | ||
} | ||
|
||
return ret, nil | ||
} |
80 changes: 80 additions & 0 deletions
80
pkg/sql/sem/builtins/pgcrypto/pgcryptocipher/cipher_method_test.go
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,80 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package pgcryptocipher | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseCipherMethod(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
// Positive tests | ||
for input, expected := range map[string]cipherMethod{ | ||
"aes": { | ||
algorithm: aesCipher, | ||
mode: cbcMode, | ||
padding: pkcsPadding, | ||
}, | ||
"aes/pad:pkcs": { | ||
algorithm: aesCipher, | ||
mode: cbcMode, | ||
padding: pkcsPadding, | ||
}, | ||
"aes/pad:none": { | ||
algorithm: aesCipher, | ||
mode: cbcMode, | ||
padding: noPadding, | ||
}, | ||
"aes-cbc": { | ||
algorithm: aesCipher, | ||
mode: cbcMode, | ||
padding: pkcsPadding, | ||
}, | ||
"aes-cbc/pad:pkcs": { | ||
algorithm: aesCipher, | ||
mode: cbcMode, | ||
padding: pkcsPadding, | ||
}, | ||
"aes-cbc/pad:none": { | ||
algorithm: aesCipher, | ||
mode: cbcMode, | ||
padding: noPadding, | ||
}, | ||
} { | ||
t.Run(input, func(t *testing.T) { | ||
ct, err := parseCipherMethod(input) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, ct) | ||
}) | ||
} | ||
|
||
// Negative tests | ||
for input, expectedErr := range map[string]string{ | ||
// Unsupported algorithms and modes | ||
"aes-ecb": `unimplemented: ECB mode is insecure and not supported`, | ||
"bf": `unimplemented: Blowfish is insecure and not supported`, | ||
|
||
// Invalid values | ||
"aes/pad=pkcs": `cipher method has wrong format: "aes/pad=pkcs"`, | ||
"aescbc": `cipher method has unsupported algorithm: "aescbc"`, | ||
"aes-ctr": `cipher method has unsupported mode: "ctr"`, | ||
"aes/pad:zero": `cipher method has unsupported padding: "zero"`, | ||
} { | ||
t.Run(input, func(t *testing.T) { | ||
_, err := parseCipherMethod(input) | ||
require.EqualError(t, err, expectedErr) | ||
}) | ||
} | ||
} |
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,13 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
// Package pgcryptocipher contains the implementation of pgcrypto | ||
// cipher functions. | ||
package pgcryptocipher |
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