-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set Istiod env COMPLIANCE_POLICY when running on a FIPS enabled OpenS…
…hift cluster (#619) * Set Istiod env when running on FIPS enabled OpenShift cluster Signed-off-by: Yuanlin Xu <[email protected]> * review updates Signed-off-by: Yuanlin Xu <[email protected]> * review update test Signed-off-by: Yuanlin Xu <[email protected]> * fix filename typo Signed-off-by: Yuanlin Xu <[email protected]> * remove fmt line Signed-off-by: Yuanlin Xu <[email protected]> * optimization: detect FIPS at the start time Signed-off-by: Yuanlin Xu <[email protected]> * review update: refactor Signed-off-by: Yuanlin Xu <[email protected]> * review update: refactor Signed-off-by: Yuanlin Xu <[email protected]> * remove log line Signed-off-by: Yuanlin Xu <[email protected]> * fix lint error Signed-off-by: Yuanlin Xu <[email protected]> --------- Signed-off-by: Yuanlin Xu <[email protected]>
- Loading branch information
Showing
4 changed files
with
205 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright Istio 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 istiovalues | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/istio-ecosystem/sail-operator/pkg/helm" | ||
) | ||
|
||
var ( | ||
FipsEnabled bool | ||
FipsEnableFilePath = "/proc/sys/crypto/fips_enabled" | ||
) | ||
|
||
// detectFipsMode checks if FIPS mode is enabled in the system. | ||
func init() { | ||
detectFipsMode(FipsEnableFilePath) | ||
} | ||
|
||
func detectFipsMode(filepath string) { | ||
contents, err := os.ReadFile(filepath) | ||
if err != nil { | ||
FipsEnabled = false | ||
} else { | ||
fipsEnabled := strings.TrimSuffix(string(contents), "\n") | ||
if fipsEnabled == "1" { | ||
FipsEnabled = true | ||
} | ||
} | ||
} | ||
|
||
// ApplyFipsValues sets value pilot.env.COMPLIANCE_POLICY if FIPS mode is enabled in the system. | ||
func ApplyFipsValues(values helm.Values) (helm.Values, error) { | ||
if FipsEnabled { | ||
if err := values.SetIfAbsent("pilot.env.COMPLIANCE_POLICY", "fips-140-2"); err != nil { | ||
return nil, fmt.Errorf("failed to set pilot.env.COMPLIANCE_POLICY: %w", err) | ||
} | ||
} | ||
return values, nil | ||
} |
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,103 @@ | ||
// Copyright Istio 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 istiovalues | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/istio-ecosystem/sail-operator/pkg/helm" | ||
) | ||
|
||
func TestDetectFipsMode(t *testing.T) { | ||
resourceDir := t.TempDir() | ||
os.WriteFile(path.Join(resourceDir, "fips_enabled"), []byte(("1\n")), 0o644) | ||
os.WriteFile(path.Join(resourceDir, "fips_not_enabled"), []byte(("0\n")), 0o644) | ||
tests := []struct { | ||
name string | ||
filepath string | ||
expectValue bool | ||
}{ | ||
{ | ||
name: "FIPS not enabled", | ||
filepath: path.Join(resourceDir, "fips_not_enabled"), | ||
expectValue: false, | ||
}, | ||
{ | ||
name: "FIPS enabled", | ||
filepath: path.Join(resourceDir, "fips_enabled"), | ||
expectValue: true, | ||
}, | ||
{ | ||
name: "file not found", | ||
filepath: path.Join(resourceDir, "fips_not_found"), | ||
expectValue: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
detectFipsMode(tt.filepath) | ||
actual := FipsEnabled | ||
|
||
if diff := cmp.Diff(tt.expectValue, actual); diff != "" { | ||
t.Errorf("FipsEnabled variable wasn't applied properly; diff (-expected, +actual):\n%v", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestApplyFipsValues(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
fipsEnabled bool | ||
expectValues helm.Values | ||
expectErr bool | ||
}{ | ||
{ | ||
name: "FIPS not enabled", | ||
fipsEnabled: false, | ||
expectValues: helm.Values{}, | ||
}, | ||
{ | ||
name: "FIPS enabled", | ||
fipsEnabled: true, | ||
expectValues: helm.Values{ | ||
"pilot": map[string]any{ | ||
"env": map[string]any{"COMPLIANCE_POLICY": string("fips-140-2")}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
values := helm.Values{} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
FipsEnabled = tt.fipsEnabled | ||
actual, err := ApplyFipsValues(values) | ||
if (err != nil) != tt.expectErr { | ||
t.Errorf("applyFipsValues() error = %v, expectErr %v", err, tt.expectErr) | ||
} | ||
|
||
if err == nil { | ||
if diff := cmp.Diff(tt.expectValues, actual); diff != "" { | ||
t.Errorf("COMPLIANCE_POLICY env wasn't applied properly; diff (-expected, +actual):\n%v", diff) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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