-
Notifications
You must be signed in to change notification settings - Fork 152
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
Enforce FIPS compatible Sprig functions for template rendering #2708
Changes from all commits
7f5e54c
04d64df
0d3fb6d
c1af301
57e2364
6cc09cf
1818118
3058510
e5fb492
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright 2024 The Kanister 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 ksprig | ||
sukhil-suresh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
|
||
"github.com/Masterminds/sprig" | ||
) | ||
|
||
// TxtFuncMap provides a FIPS compliant version of sprig.TxtFuncMap(). | ||
// Usage of a FIPS non-compatible function from the function map will result in an error. | ||
func TxtFuncMap() template.FuncMap { | ||
return replaceNonCompliantFuncs(sprig.TxtFuncMap()) | ||
} | ||
|
||
func replaceNonCompliantFuncs(m map[string]interface{}) map[string]interface{} { | ||
for name, fn := range fipsNonCompliantFuncs { | ||
if _, ok := m[name]; ok { | ||
m[name] = fn | ||
} | ||
} | ||
return m | ||
} | ||
|
||
// fipsNonCompliantFuncs is a map of sprig function name to its replacement function. | ||
// Functions identified for Sprig v3.2.3. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How are we ensuring that future bumps of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The kanister project uses an older There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea, maybe we need to add a file like |
||
var fipsNonCompliantFuncs = map[string]interface{}{ | ||
"bcrypt": func(input string) (string, error) { | ||
return "", NewUnsupportedSprigUsageErr("bcrypt") | ||
}, | ||
|
||
"derivePassword": func(counter uint32, password_type, password, user, site string) (string, error) { | ||
return "", NewUnsupportedSprigUsageErr("derivePassword") | ||
}, | ||
|
||
"genPrivateKey": func(typ string) (string, error) { | ||
switch typ { | ||
case "rsa", "ecdsa", "ed25519": | ||
fn, ok := sprig.TxtFuncMap()["genPrivateKey"].(func(string) string) | ||
if !ok { | ||
return "", NewUnsupportedSprigUsageErr("genPrivateKey") | ||
} | ||
return fn(typ), nil | ||
} | ||
return "", NewUnsupportedSprigUsageErr(fmt.Sprintf("genPrivateKey for %s", typ)) | ||
}, | ||
|
||
"htpasswd": func(username string, password string) (string, error) { | ||
return "", NewUnsupportedSprigUsageErr("htpasswd") | ||
}, | ||
} | ||
|
||
// NewUnsupportedSprigUsageErr returns an UnsupportedSprigUsageErr. | ||
func NewUnsupportedSprigUsageErr(usage string) UnsupportedSprigUsageErr { | ||
return UnsupportedSprigUsageErr{Usage: usage} | ||
} | ||
|
||
// UnsupportedSprigUsageErr indicates a FIPS non-compatible sprig usage. | ||
type UnsupportedSprigUsageErr struct { | ||
Usage string | ||
} | ||
|
||
// Error returns an error string indicating the unsupported function. | ||
func (err UnsupportedSprigUsageErr) Error() string { | ||
return fmt.Sprintf("sprig usage of '%s' is not allowed by kanister as it is not FIPS compatible", err.Usage) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2024 The Kanister 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 ksprig_test | ||
sukhil-suresh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
"text/template" | ||
|
||
. "gopkg.in/check.v1" | ||
|
||
"github.com/kanisterio/kanister/pkg/ksprig" | ||
) | ||
|
||
type FipsOnlySprigSuite struct{} | ||
|
||
var _ = Suite(&FipsOnlySprigSuite{}) | ||
|
||
func TestFipsOnlySprigSuite(t *testing.T) { TestingT(t) } | ||
|
||
func (f *FipsOnlySprigSuite) TestUnsupportedTxtFuncMapUsage(c *C) { | ||
funcMap := ksprig.TxtFuncMap() | ||
|
||
testCases := []struct { | ||
function string | ||
templateText string | ||
usageErr string | ||
}{ | ||
{ | ||
function: "bcrypt", | ||
templateText: "{{bcrypt \"password\"}}", | ||
usageErr: "bcrypt", | ||
}, | ||
{ | ||
function: "derivePassword", | ||
templateText: "{{derivePassword 1 \"long\" \"password\" \"user\" \"example.com\"}}", | ||
usageErr: "derivePassword", | ||
}, | ||
{ | ||
function: "genPrivateKey", | ||
templateText: "{{genPrivateKey \"dsa\"}}", | ||
usageErr: "genPrivateKey for dsa", | ||
}, | ||
{ | ||
function: "htpasswd", | ||
templateText: "{{htpasswd \"username\" \"password\"}}", | ||
usageErr: "htpasswd", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
if _, ok := funcMap[tc.function]; !ok { | ||
c.Logf("Skipping test of %s since the tested sprig version does not support it", tc.function) | ||
continue | ||
} | ||
c.Logf("Testing %s", tc.function) | ||
|
||
temp, err := template.New("test").Funcs(funcMap).Parse(tc.templateText) | ||
c.Assert(err, IsNil) | ||
|
||
err = temp.Execute(nil, "") | ||
|
||
var sprigErr ksprig.UnsupportedSprigUsageErr | ||
c.Assert(errors.As(err, &sprigErr), Equals, true) | ||
c.Assert(sprigErr.Usage, Equals, tc.usageErr) | ||
} | ||
} | ||
|
||
func (f *FipsOnlySprigSuite) TestSupportedTxtFuncMapUsage(c *C) { | ||
funcMap := ksprig.TxtFuncMap() | ||
|
||
testCases := []struct { | ||
description string | ||
function string | ||
templateText string | ||
}{ | ||
// The supported usages are not limited to these test cases | ||
{ | ||
description: "genPrivateKey for rsa key", | ||
function: "genPrivateKey", | ||
templateText: "{{genPrivateKey \"rsa\"}}", | ||
}, | ||
{ | ||
description: "genPrivateKey for ecdsa key", | ||
function: "genPrivateKey", | ||
templateText: "{{genPrivateKey \"ecdsa\"}}", | ||
}, | ||
{ | ||
description: "genPrivateKey for ed25519 key", | ||
function: "genPrivateKey", | ||
templateText: "{{genPrivateKey \"ed25519\"}}", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
if _, ok := funcMap[tc.function]; !ok { | ||
c.Logf("Skipping test of %s since the tested sprig version does not support it", tc.function) | ||
continue | ||
} | ||
c.Logf("Testing %s", tc.description) | ||
|
||
temp, err := template.New("test").Funcs(funcMap).Parse(tc.templateText) | ||
c.Assert(err, IsNil) | ||
|
||
err = temp.Execute(&strings.Builder{}, "") | ||
c.Assert(err, IsNil) | ||
} | ||
} |
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.
How do we ensure we use this file in place of
sprig
in the future?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.
I can only think of exploring a custom linter rule for this purpose. We need an agreement on the solution and if it needs to be addressed in this PR.
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.
Yea, I don't think this comment or #2708 (comment) should block this PR. I brought it up as a concern but if we agree to address it moving forward then that's fine.