kubernetes.go 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package rules
  2. import (
  3. "regexp"
  4. "github.com/zricethezav/gitleaks/v8/config"
  5. )
  6. // The kubernetes rules are split into two functions to make the complex proximity matching of the data-key and the kind-identifier more readable and testable
  7. // KubernetesSecretWithDataBefore validates if we detected a kubernetes secret which contains data, before the resource identifier!
  8. func KubernetesSecretWithDataBefore() *config.Rule {
  9. // define rule
  10. r := config.Rule{
  11. RuleID: "kubernetes-secret-with-data-before",
  12. Description: "Possible Kubernetes Secret detected, posing a risk of leaking credentials/tokens from your deployments",
  13. // We try to match secrets by looking if we have the keyword
  14. Regex: generateUniqueTokenRegex(`(?i)(?:\b(?:data:))(\W+(?:\w+\W+){0,200}?)\bkind:.{0,10}Secret\b`, true),
  15. Keywords: []string{
  16. "Secret",
  17. },
  18. // Kubernetes secrets are always yaml files, we limit to common yaml-endings to make this rule more safe!
  19. Path: regexp.MustCompile(`(?i)\.ya?ml$`),
  20. }
  21. // validate
  22. tps := map[string]string{
  23. // Sample Kubernetes Secret from https://kubernetes.io/docs/concepts/configuration/secret/
  24. // These secrets contain the "data"-key before the actual identifier "kind: Secret"
  25. "kubernetes.yaml": "apiVersion: v1'\n' data:'\n' extra: YmFyCg=='\n' kind: secret'\n' metadata:'\n' name: secret-sa-sample'\n' annotations:'\n' kubernetes.io/service-account.name: 'sa-name'", // gitleaks:allow
  26. "kubernetes.yml": "apiVersion: v1'\n' data:'\n' password: UyFCXCpkJHpEc2I9'\n' username: YWRtaW4='\n' kind: Secret'\n' metadata:'\n' creationTimestamp: '2022-06-28T17:44:13Z''\n' name: db-user-pass'\n' namespace: default'\n' type: Opaque", // gitleaks:allow
  27. // Quoted Test Cases
  28. "kubernetes-quoted-1.yaml": "apiVersion: v1'\n' data:'\n' extra: YmFyCg=='\n' kind: 'Secret''\n' metadata:'\n' name: 'secret-sa-sample''\n' annotations:'\n' kubernetes.io/service-account.name: 'sa-name'", // gitleaks:allow
  29. "kubernetes-quoted-2.yaml": "apiVersion: v1'\n' data:'\n' extra: YmFyCg=='\n' kind: 'secret''\n' metadata:'\n' name: 'secret-sa-sample''\n' annotations:'\n' kubernetes.io/service-account.name: 'sa-name'", // gitleaks:allow
  30. }
  31. return validateWithPaths(r, tps, nil)
  32. }
  33. // KubernetesSecretWithDataAfter validates if we detected a kubernetes secret which contains data, after the resource identifier!
  34. func KubernetesSecretWithDataAfter() *config.Rule {
  35. // define rule
  36. r := config.Rule{
  37. RuleID: "kubernetes-secret-with-data-after",
  38. Description: "Possible Kubernetes Secret detected, posing a risk of leaking credentials/tokens from your deployments",
  39. // We try to match secrets by looking if we have the keyword
  40. Regex: generateUniqueTokenRegex(`(?i)(?:\bkind:.{0,10}Secret\b)(?:.|\s){0,200}?\b(?:data:)\s*(.+)`, true),
  41. Keywords: []string{
  42. "Secret",
  43. },
  44. // Kubernetes secrets are always yaml files, we limit to common yaml-endings to make this rule more safe!
  45. Path: regexp.MustCompile(`(?i)\.ya?ml$`),
  46. }
  47. // validate
  48. tps := map[string]string{
  49. // Sample Kubernetes Secret from https://kubernetes.io/docs/concepts/configuration/secret/
  50. // These secrets contain the data after the actual identifier "kind: Secret"
  51. "kubernetes.yaml": "apiVersion: v1'\n' kind: secret'\n' data:'\n' extra: YmFyCg=='\n' metadata:'\n' name: secret-sa-sample'\n' annotations:'\n' kubernetes.io/service-account.name: 'sa-name'", // gitleaks:allow
  52. "kubernetes.yml": "apiVersion: v1'\n' kind: Secret'\n' data:'\n' password: UyFCXCpkJHpEc2I9'\n' username: YWRtaW4='\n' metadata:'\n' creationTimestamp: '2022-06-28T17:44:13Z''\n' name: db-user-pass'\n' namespace: default'\n' type: Opaque", // gitleaks:allow
  53. // Quoted Test Cases
  54. "kubernetes-quoted-1.yaml": "apiVersion: v1'\n' kind: 'Secret''\n' data:'\n' password: UyFCXCpkJHpEc2I9'\n' username: YWRtaW4='\n' metadata:'\n' name: db-user-pass'\n' namespace: default'\n' type: Opaque", // gitleaks:allow
  55. "kubernetes-quoted-2.yaml": "apiVersion: v1'\n' kind: 'secret''\n' data:'\n' password: UyFCXCpkJHpEc2I9'\n' username: YWRtaW4='\n' metadata:'\n' name: db-user-pass'\n' namespace: default'\n' type: Opaque", // gitleaks:allow
  56. }
  57. return validateWithPaths(r, tps, nil)
  58. }