15
15
package fips
16
16
17
17
import (
18
- "errors"
19
- "fmt"
20
- "os"
21
18
"strings"
22
19
)
23
20
24
- const fipsFile = "/proc/sys/crypto/fips_enabled"
21
+ type FIPSCheck interface {
22
+ DisabledComponents (receivers map [string ]interface {}, exporters map [string ]interface {}, processors map [string ]interface {}, extensions map [string ]interface {}) []string
23
+ }
25
24
26
25
// FipsCheck holds configuration for FIPS black list.
27
- type FipsCheck struct {
26
+ type fipsCheck struct {
28
27
isFIPSEnabled bool
29
28
30
29
receivers map [string ]bool
@@ -33,15 +32,24 @@ type FipsCheck struct {
33
32
extensions map [string ]bool
34
33
}
35
34
35
+ type noopFIPSCheck struct {}
36
+
37
+ func (noopFIPSCheck ) DisabledComponents (receivers map [string ]interface {}, exporters map [string ]interface {}, processors map [string ]interface {}, extensions map [string ]interface {}) []string {
38
+ return nil
39
+ }
40
+
36
41
// NewFipsCheck creates new FipsCheck.
37
42
// It checks if FIPS is enabled on the platform in /proc/sys/crypto/fips_enabled.
38
- func NewFipsCheck (receivers , exporters , processors , extensions []string ) FipsCheck {
39
- return FipsCheck {
40
- isFIPSEnabled : isFipsEnabled (),
41
- receivers : listToMap (receivers ),
42
- exporters : listToMap (exporters ),
43
- processors : listToMap (processors ),
44
- extensions : listToMap (extensions ),
43
+ func NewFipsCheck (FIPSEnabled bool , receivers , exporters , processors , extensions []string ) FIPSCheck {
44
+ if ! FIPSEnabled {
45
+ return & noopFIPSCheck {}
46
+ }
47
+
48
+ return & fipsCheck {
49
+ receivers : listToMap (receivers ),
50
+ exporters : listToMap (exporters ),
51
+ processors : listToMap (processors ),
52
+ extensions : listToMap (extensions ),
45
53
}
46
54
}
47
55
@@ -54,27 +62,24 @@ func listToMap(list []string) map[string]bool {
54
62
}
55
63
56
64
// Check checks if a submitted components are back lister or not.
57
- func (fips FipsCheck ) Check (receivers map [string ]interface {}, exporters map [string ]interface {}, processors map [string ]interface {}, extensions map [string ]interface {}) []string {
58
- if ! fips .isFIPSEnabled {
59
- return nil
60
- }
65
+ func (fips fipsCheck ) DisabledComponents (receivers map [string ]interface {}, exporters map [string ]interface {}, processors map [string ]interface {}, extensions map [string ]interface {}) []string {
61
66
var disabled []string
62
- if comp := isBlackListed (fips .receivers , receivers ); comp != "" {
67
+ if comp := isDisabled (fips .receivers , receivers ); comp != "" {
63
68
disabled = append (disabled , comp )
64
69
}
65
- if comp := isBlackListed (fips .exporters , exporters ); comp != "" {
70
+ if comp := isDisabled (fips .exporters , exporters ); comp != "" {
66
71
disabled = append (disabled , comp )
67
72
}
68
- if comp := isBlackListed (fips .processors , processors ); comp != "" {
73
+ if comp := isDisabled (fips .processors , processors ); comp != "" {
69
74
disabled = append (disabled , comp )
70
75
}
71
- if comp := isBlackListed (fips .extensions , extensions ); comp != "" {
76
+ if comp := isDisabled (fips .extensions , extensions ); comp != "" {
72
77
disabled = append (disabled , comp )
73
78
}
74
79
return disabled
75
80
}
76
81
77
- func isBlackListed (blackListed map [string ]bool , cfg map [string ]interface {}) string {
82
+ func isDisabled (blackListed map [string ]bool , cfg map [string ]interface {}) string {
78
83
for id := range cfg {
79
84
component := strings .Split (id , "/" )[0 ]
80
85
if blackListed [component ] {
@@ -83,20 +88,3 @@ func isBlackListed(blackListed map[string]bool, cfg map[string]interface{}) stri
83
88
}
84
89
return ""
85
90
}
86
-
87
- func isFipsEnabled () bool {
88
- // check if file exists
89
- if _ , err := os .Stat (fipsFile ); errors .Is (err , os .ErrNotExist ) {
90
- fmt .Println ("fips file doesn't exist" )
91
- return false
92
- }
93
- content , err := os .ReadFile (fipsFile )
94
- if err != nil {
95
- // file cannot be read, enable FIPS to avoid any violations
96
- fmt .Println ("cannot read fips file" )
97
- return true
98
- }
99
- contentStr := string (content )
100
- contentStr = strings .TrimSpace (contentStr )
101
- return contentStr == "1"
102
- }
0 commit comments