-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce FIPS compatible Sprig functions for template rendering (#2708)
* Add fipsonly sprig support * Add tests for fipsonly sprig * Update usages of sprig * Update docs about rendering templates * Update docs/templates.rst Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com> * Update pkg/ksprig/fipsonly_sprig.go Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com> * Update pkg/ksprig/fipsonly_sprig_test.go Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com> * Rename error and error field * Convert tests to check.v1 format --------- Co-authored-by: Pavan Navarathna <6504783+pavannd1@users.noreply.github.com>
- Loading branch information
1 parent
282c5bd
commit af11989
Showing
7 changed files
with
214 additions
and
9 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 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 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 | ||
|
||
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. | ||
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) | ||
} |
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,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 | ||
|
||
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) | ||
} | ||
} |
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 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