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

RequestReply: Add CESQL function #8360

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
227 changes: 227 additions & 0 deletions pkg/eventfilter/subscriptionsapi/cesql_correlationid_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
/*
Copyright 2022 The Knative Authors

Licensed 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.
*/

package subscriptionsapi

import (
"context"
"crypto/aes"
"crypto/cipher"
"crypto/des"

Check failure on line 23 in pkg/eventfilter/subscriptionsapi/cesql_correlationid_filter.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

G502: Blocklisted import crypto/des: weak cryptographic primitive (gosec)
"crypto/rc4"

Check failure on line 24 in pkg/eventfilter/subscriptionsapi/cesql_correlationid_filter.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

G503: Blocklisted import crypto/rc4: weak cryptographic primitive (gosec)
"encoding/base64"
"encoding/hex"
"errors"
"os"
"path"
"regexp"
"strings"

cloudevents "github.com/cloudevents/sdk-go/v2"
"knative.dev/eventing/pkg/eventfilter"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"

cesql "github.com/cloudevents/sdk-go/sql/v2"
cefn "github.com/cloudevents/sdk-go/sql/v2/function"
ceruntime "github.com/cloudevents/sdk-go/sql/v2/runtime"
)

// Add a user defined function to validate correlation id, then return a cesql_filter
func NewCESQLCorrelationIdFilter(expr string) (eventfilter.Filter, error) {

var correlationIdFilterFunction = cefn.NewFunction(
"KN_VERIFY_CORRELATIONID",
[]cesql.Type{cesql.StringType},
cesql.TypePtr(cesql.StringType),
cesql.BooleanType,
func(event cloudevents.Event, i []interface{}) (interface{}, error) {
correlationId := i[0].(string)

match, _ := regexp.MatchString(".*:.*", correlationId)
if !match {
return false, errors.New("correlationId Format: <original id>:<base64/hex encoded encrypted id>")
}

slice := strings.Split(correlationId, ":")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the split here, do we need to consider that : may be a part of the id itself? Is there a need for this to be configurable? cc @pierDipi @creydr

originalId := slice[0]
encryptedId := slice[1]

encryptedIdBytes, err := decodeBase64OrHex(encryptedId)
if err != nil {
return false, err
}

// Create a set of secret names to try looking for in k8s
secretNamesToTry := make(map[string]bool)

// Iterate through secret names in argument list and add them to the set
for num, secret := range i {
if num != 0 {
secretNamesToTry[secret.(string)] = true
}
}

secrets, err := getSecretsFromK8s()
if err != nil {
return false, err
}

/*
* Go through each retrived secret

Check failure on line 87 in pkg/eventfilter/subscriptionsapi/cesql_correlationid_filter.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

"retrived" is a misspelling of "retrieved"

Check failure on line 87 in pkg/eventfilter/subscriptionsapi/cesql_correlationid_filter.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

[github.com/client9/misspell] reported by reviewdog 🐶 "retrived" is a misspelling of "retrieved" Raw Output: pkg/eventfilter/subscriptionsapi/cesql_correlationid_filter.go:87:22: "retrived" is a misspelling of "retrieved"
* Check if the secret has data for a key and algorithm
* Check if the secret name matches with one of the arguments
* Decrypt encryptedId using the current secret's key
* Check if the encrypted value matches with originalId
*/
for _, secret := range secrets {
key, keyFieldExists := secret.Data["key"]
algorithm, algorithmFieldExists := secret.Data["algorithm"]

if keyFieldExists && algorithmFieldExists && secretNamesToTry[secret.Name] {
var decryptionFunc func(originalId string, encryptedIdBytes []byte, key []byte) (bool, error)
switch strings.ToUpper(string(algorithm)) {
case "AES", "AES-ECB":
decryptionFunc = compareWithAES
case "DES":
decryptionFunc = compareWithDES
case "3DES", "TRIPLEDES":
decryptionFunc = compareWithTripleDES
case "RC4":
decryptionFunc = compareWithRC4
default:
return false, errors.New("cipher algorithm not supported")
}

res, err := decryptionFunc(originalId, encryptedIdBytes, key)
if err != nil {
return false, err
}
if res {
return true, nil
}
}
Comment on lines +94 to +119
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might split this out into its own function

}

return false, nil
},
)

ceruntime.AddFunction(correlationIdFilterFunction)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might add this to the runtime somewhere else, since this will be called to make a new CESQL expression as well. The function is doing a bit too much. There should be 2 functions:

  1. Make and add the CESQL function
  2. Parse the CESQL expression into a filter


return NewCESQLFilter(expr)
}

func getSecretsFromK8s() ([]v1.Secret, error) {
// Assuming .kube/config is in the home directory
basePath, _ := os.UserHomeDir()
defaultKubeConfigPath := path.Join(basePath, ".kube", "config")

// Set up k8s client to get secrets
config, err := clientcmd.BuildConfigFromFlags("", defaultKubeConfigPath)
if err != nil {
return nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
Comment on lines +132 to +144
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is done for you, you can import

kubeclient "knative.dev/pkg/client/injection/kube/client"
and use it similarly to get the kube config

secrets, err := clientset.CoreV1().Secrets("default").List(context.Background(), metav1.ListOptions{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably need to configure:

  1. which namespace to look up secrets in
  2. which secret names to get

if err != nil {
return nil, err
}

return secrets.Items, nil
}

func decodeBase64OrHex(encryptedId string) ([]byte, error) {
hexRegex := regexp.MustCompile("^([0-9A-Fa-f]+)$")
base64Regex := regexp.MustCompile(`^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$`)

encryptedIdBytes := []byte(encryptedId)

if hexRegex.Match(encryptedIdBytes) {
return hex.DecodeString(encryptedId)
} else if base64Regex.Match(encryptedIdBytes) {
return base64.StdEncoding.DecodeString(encryptedId)
} else {
return nil, errors.New("encryptedId must either be Base64 or Hex encoded")
}
}

func compareWithAES(originalId string, encryptedIdBytes []byte, key []byte) (bool, error) {
block, err := aes.NewCipher(key)
if err != nil {
return false, err
}
plainText := getPlaintextFromBlock(block, encryptedIdBytes)

return plainText == originalId, nil
}

func compareWithDES(originalId string, encryptedIdBytes []byte, key []byte) (bool, error) {
block, err := des.NewCipher(key)

Check failure on line 179 in pkg/eventfilter/subscriptionsapi/cesql_correlationid_filter.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

G401: Use of weak cryptographic primitive (gosec)
if err != nil {
return false, nil
}
plainText := getPlaintextFromBlock(block, encryptedIdBytes)

return plainText == originalId, nil
}

func compareWithTripleDES(originalId string, encryptedIdBytes []byte, key []byte) (bool, error) {
block, err := des.NewTripleDESCipher(key)

Check failure on line 189 in pkg/eventfilter/subscriptionsapi/cesql_correlationid_filter.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

G401: Use of weak cryptographic primitive (gosec)
if err != nil {
return false, nil
}
plainText := getPlaintextFromBlock(block, encryptedIdBytes)

return plainText == originalId, nil
}

func compareWithRC4(originalId string, encryptedIdBytes []byte, key []byte) (bool, error) {
cipher, err := rc4.NewCipher(key)

Check failure on line 199 in pkg/eventfilter/subscriptionsapi/cesql_correlationid_filter.go

View workflow job for this annotation

GitHub Actions / style / Golang / Lint

G401: Use of weak cryptographic primitive (gosec)
if err != nil {
return false, nil
}
out := make([]byte, len(encryptedIdBytes))
cipher.XORKeyStream(out, encryptedIdBytes)
plainText := string(out)

return plainText == originalId, nil
}

func getPlaintextFromBlock(block cipher.Block, encryptedIdBytes []byte) string {
plainText := make([]byte, len(encryptedIdBytes))
for i, j := 0, block.BlockSize(); i < len(encryptedIdBytes); i, j = i+block.BlockSize(), j+block.BlockSize() {
block.Decrypt(plainText[i:j], encryptedIdBytes[i:j])
}
trim := 0
if len(plainText) > 0 {
trim = len(plainText) - int(plainText[len(plainText)-1])
}

if trim >= 0 {
trimmedPlaintext := string(plainText[:trim])

return trimmedPlaintext
}

return ""
}
Loading
Loading