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

Enable agent to detect FIPS enabled hosts #4189

Merged
merged 5 commits into from
May 28, 2024
Merged
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
8 changes: 8 additions & 0 deletions agent/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ var (

// CgroupV2 Specifies whether or not to run in Cgroups V2 mode.
CgroupV2 = false

// isFIPSEnabled indicates whether FIPS mode is enabled on the host
isFIPSEnabled = false
)

// Merge merges two config files, preferring the ones on the left. Any nil or
Expand Down Expand Up @@ -226,6 +229,7 @@ func NewConfig(ec2client ec2.EC2MetadataClient) (*Config, error) {
errs = append(errs, err)
}
config := &envConfig
isFIPSEnabled = utils.DetectFIPSMode(utils.FIPSModeFilePath)

if config.External.Enabled() {
if config.AWSRegion == "" {
Expand Down Expand Up @@ -657,3 +661,7 @@ func (cfg *Config) String() string {
cfg.platformString(),
)
}

func IsFIPSEnabled() bool {
singholt marked this conversation as resolved.
Show resolved Hide resolved
return isFIPSEnabled
}
45 changes: 45 additions & 0 deletions agent/utils/fips_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build linux
// +build linux

// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 utils

import (
"fmt"
"os"
"strings"

"github.com/aws/amazon-ecs-agent/ecs-agent/logger"
)

const FIPSModeFilePath = "/proc/sys/crypto/fips_enabled"

// DetectFIPSMode checks if FIPS mode is enabled based on the provided file path.
func DetectFIPSMode(filePath string) bool {
data, err := os.ReadFile(filePath)
singholt marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logger.Debug(fmt.Sprintf("Error while detecting FIPS is enabled or not, err: %v", err))
return false
}

fipsValue := strings.TrimSpace(string(data))
if fipsValue == "1" {
logger.Info("FIPS mode detected on the host")
return true
}

logger.Debug(fmt.Sprintf("FIPS mode not enabled. FIPS mode explicitly set to %v", fipsValue))
return false
}
52 changes: 52 additions & 0 deletions agent/utils/fips_linux_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//go:build linux && unit
// +build linux,unit

// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 utils

import (
"io/ioutil"
harishxr marked this conversation as resolved.
Show resolved Hide resolved
"log"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDetectFIPSMode(t *testing.T) {
harishxr marked this conversation as resolved.
Show resolved Hide resolved
// Create a temporary file to mock the FIPS mode file
tempFile, err := ioutil.TempFile("", "fips_enabled")
assert.NoError(t, err)
defer os.Remove(tempFile.Name())
// Test FIPS mode enabled
_, err = tempFile.WriteString("1\n")
assert.NoError(t, err)
tempFile.Sync()
// Initialize the logger
log.SetFlags(log.LstdFlags | log.Lshortfile)
result := DetectFIPSMode(tempFile.Name())
assert.True(t, result, "FIPS mode should be detected")
// Test FIPS mode disabled
tempFile.Truncate(0)
tempFile.Seek(0, 0)
_, err = tempFile.WriteString("0\n")
assert.NoError(t, err)
tempFile.Sync()
result = DetectFIPSMode(tempFile.Name())
assert.False(t, result, "FIPS mode should not be detected")
// Test when the FIPS file does not exist
result = DetectFIPSMode("nonexistent_file")
assert.False(t, result, "FIPS mode should not be detected when file is missing")
}
28 changes: 28 additions & 0 deletions agent/utils/fips_unsupported.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build !windows && !linux
// +build !windows,!linux

// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 utils

import (
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"
)

const FIPSModeFilePath = ""

func DetectFIPSMode(filepath string) bool {
logger.Debug("FIPS mode detection is not supported on this platform")
return false
}
28 changes: 28 additions & 0 deletions agent/utils/fips_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build windows && !linux
// +build windows,!linux

// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 utils

import (
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"
)

const FIPSModeFilePath = ""

func DetectFIPSMode(filepath string) bool {
logger.Debug("set isFIPSEnabled to false by default on Windows")
return false
}
Loading