| 1 | // Copyright 2020 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package ext |
| 16 | |
| 17 | import ( |
| 18 | "encoding/base64" |
| 19 | "math" |
| 20 | |
| 21 | "github.com/google/cel-go/cel" |
| 22 | "github.com/google/cel-go/common/types" |
| 23 | "github.com/google/cel-go/common/types/ref" |
| 24 | ) |
| 25 | |
| 26 | // Encoders returns a cel.EnvOption to configure extended functions for string, byte, and object |
| 27 | // encodings. |
| 28 | // |
| 29 | // # Base64.Decode |
| 30 | // |
| 31 | // Decodes base64-encoded string to bytes. |
| 32 | // |
| 33 | // This function will return an error if the string input is not base64-encoded. |
| 34 | // |
| 35 | // base64.decode(<string>) -> <bytes> |
| 36 | // |
| 37 | // Examples: |
| 38 | // |
| 39 | // base64.decode('aGVsbG8=') // return b'hello' |
| 40 | // base64.decode('aGVsbG8') // return b'hello' |
| 41 | // |
| 42 | // # Base64.Encode |
| 43 | // |
| 44 | // Encodes bytes to a base64-encoded string. |
| 45 | // |
| 46 | // base64.encode(<bytes>) -> <string> |
| 47 | // |
| 48 | // Examples: |
| 49 | // |
| 50 | // base64.encode(b'hello') // return b'aGVsbG8=' |
| 51 | func Encoders(options ...EncodersOption) cel.EnvOption { |
| 52 | l := &encoderLib{version: math.MaxUint32} |
| 53 | for _, o := range options { |
| 54 | l = o(l) |
| 55 | } |
| 56 | return cel.Lib(l) |
| 57 | } |
| 58 | |
| 59 | // EncodersOption declares a functional operator for configuring encoder extensions. |
| 60 | type EncodersOption func(*encoderLib) *encoderLib |
| 61 | |
| 62 | // EncodersVersion sets the library version for encoder extensions. |
| 63 | func EncodersVersion(version uint32) EncodersOption { |
| 64 | return func(lib *encoderLib) *encoderLib { |
| 65 | lib.version = version |
| 66 | return lib |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | type encoderLib struct { |
| 71 | version uint32 |
| 72 | } |
| 73 | |
| 74 | func (*encoderLib) LibraryName() string { |
| 75 | return "cel.lib.ext.encoders" |
| 76 | } |
| 77 | |
| 78 | func (*encoderLib) CompileOptions() []cel.EnvOption { |
| 79 | return []cel.EnvOption{ |
| 80 | cel.Function("base64.decode", |
| 81 | cel.Overload("base64_decode_string", []*cel.Type{cel.StringType}, cel.BytesType, |
| 82 | cel.UnaryBinding(func(str ref.Val) ref.Val { |
| 83 | s := str.(types.String) |
| 84 | return bytesOrError(base64DecodeString(string(s))) |
| 85 | }))), |
| 86 | cel.Function("base64.encode", |
| 87 | cel.Overload("base64_encode_bytes", []*cel.Type{cel.BytesType}, cel.StringType, |
| 88 | cel.UnaryBinding(func(bytes ref.Val) ref.Val { |
| 89 | b := bytes.(types.Bytes) |
| 90 | return stringOrError(base64EncodeBytes([]byte(b))) |
| 91 | }))), |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func (*encoderLib) ProgramOptions() []cel.ProgramOption { |
| 96 | return []cel.ProgramOption{} |
| 97 | } |
| 98 | |
| 99 | func base64DecodeString(str string) ([]byte, error) { |
| 100 | b, err := base64.StdEncoding.DecodeString(str) |
| 101 | if err == nil { |
| 102 | return b, nil |
| 103 | } |
| 104 | if _, tryAltEncoding := err.(base64.CorruptInputError); tryAltEncoding { |
| 105 | return base64.RawStdEncoding.DecodeString(str) |
| 106 | } |
| 107 | return nil, err |
| 108 | } |
| 109 | |
| 110 | func base64EncodeBytes(bytes []byte) (string, error) { |
| 111 | return base64.StdEncoding.EncodeToString(bytes), nil |
| 112 | } |