|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package fips |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "strings" |
| 22 | +) |
| 23 | + |
| 24 | +const fipsFile = "/proc/sys/crypto/fips_enabled" |
| 25 | + |
| 26 | +// FipsCheck holds configuration for FIPS black list. |
| 27 | +type FipsCheck struct { |
| 28 | + isFIPSEnabled bool |
| 29 | + |
| 30 | + receivers map[string]bool |
| 31 | + exporters map[string]bool |
| 32 | + processors map[string]bool |
| 33 | + extensions map[string]bool |
| 34 | +} |
| 35 | + |
| 36 | +// NewFipsCheck creates new FipsCheck. |
| 37 | +// 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), |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func listToMap(list []string) map[string]bool { |
| 49 | + m := map[string]bool{} |
| 50 | + for _, v := range list { |
| 51 | + m[v] = true |
| 52 | + } |
| 53 | + return m |
| 54 | +} |
| 55 | + |
| 56 | +// 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 | + } |
| 61 | + var disabled []string |
| 62 | + if comp := isBlackListed(fips.receivers, receivers); comp != "" { |
| 63 | + disabled = append(disabled, comp) |
| 64 | + } |
| 65 | + if comp := isBlackListed(fips.exporters, exporters); comp != "" { |
| 66 | + disabled = append(disabled, comp) |
| 67 | + } |
| 68 | + if comp := isBlackListed(fips.processors, processors); comp != "" { |
| 69 | + disabled = append(disabled, comp) |
| 70 | + } |
| 71 | + if comp := isBlackListed(fips.extensions, extensions); comp != "" { |
| 72 | + disabled = append(disabled, comp) |
| 73 | + } |
| 74 | + return disabled |
| 75 | +} |
| 76 | + |
| 77 | +func isBlackListed(blackListed map[string]bool, cfg map[string]interface{}) string { |
| 78 | + for id, _ := range cfg { |
| 79 | + component := strings.Split(id, "/")[0] |
| 80 | + if blackListed[component] { |
| 81 | + return component |
| 82 | + } |
| 83 | + } |
| 84 | + return "" |
| 85 | +} |
| 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