-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: Pull Request resolved: #1335 ## What * Create `SecretScrubber` to scrub secrets (defined by regex patterns) out of text * Create logging formatter so that we can scrub each line before we log it * Create PC-CLI endpoint for ad-hoc scrubbing (e.g. instance files, config.yml files) ## Why * logging secrets is bad Reviewed By: gorel Differential Revision: D38074790 fbshipit-source-id: 2cbb5957b50bc4f0b86d5ef3a6fb9dc7eae2469a
- Loading branch information
1 parent
be82624
commit 6f68f81
Showing
4 changed files
with
156 additions
and
7 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,69 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
# pyre-strict | ||
|
||
import logging | ||
import re | ||
from dataclasses import dataclass | ||
from typing import Dict, List | ||
|
||
|
||
@dataclass | ||
class Secret: | ||
name: str | ||
regex_pattern_str: str | ||
|
||
|
||
@dataclass | ||
class ScrubSummary: | ||
scrubbed_output: str | ||
total_substitutions: int | ||
name_to_num_subs: Dict[str, int] | ||
|
||
def get_report(self) -> str: | ||
report = "Scrub Summary\n" | ||
for name, num_subs in self.name_to_num_subs.items(): | ||
report += f"\n* {name}: {num_subs} substitutions" | ||
report += f"\n* Total substitutions: {self.total_substitutions}" | ||
return report | ||
|
||
|
||
class SecretScrubber: | ||
SECRETS: List[Secret] = [ | ||
# https://aws.amazon.com/blogs/security/a-safer-way-to-distribute-aws-credentials-to-ec2/ | ||
Secret("AWS access key id", "(?<![A-Z0-9])[A-Z0-9]{20}(?![A-Z0-9])"), | ||
# https://aws.amazon.com/blogs/security/a-safer-way-to-distribute-aws-credentials-to-ec2/ | ||
Secret( | ||
"AWS secret access key", | ||
"(?<![A-Za-z0-9/+=])[A-Za-z0-9/+=]{40}(?![A-Za-z0-9/+=])", | ||
), | ||
# https://fburl.com/code/ogf6f0v4 | ||
Secret("Meta Graph API token", "([^a-zA-Z0-9]|^)EAA[a-zA-Z0-9]{90,400}"), | ||
] | ||
REPLACEMENT_STR: str = "********" | ||
|
||
def __init__(self) -> None: | ||
self.patterns: Dict[str, re.Pattern] = { | ||
secret.name: re.compile(secret.regex_pattern_str) for secret in self.SECRETS | ||
} | ||
|
||
def scrub(self, string: str) -> ScrubSummary: | ||
total_substitutions = 0 | ||
name_to_num_subs = {} | ||
for name, pattern in self.patterns.items(): | ||
string, num_substitutes = pattern.subn(self.REPLACEMENT_STR, string) | ||
name_to_num_subs[name] = num_substitutes | ||
total_substitutions += num_substitutes | ||
return ScrubSummary(string, total_substitutions, name_to_num_subs) | ||
|
||
|
||
class LoggingSecretScrubber(logging.Formatter): | ||
SECRET_SCRUBBER: SecretScrubber = SecretScrubber() | ||
|
||
def format(self, record: logging.LogRecord) -> str: | ||
original = logging.Formatter.format(self, record) | ||
return self.SECRET_SCRUBBER.scrub(original).scrubbed_output |
56 changes: 56 additions & 0 deletions
56
fbpcs/private_computation/test/service/test_secret_scrubber.py
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,56 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from unittest import TestCase | ||
|
||
from fbpcs.private_computation.service.secret_scrubber import SecretScrubber | ||
|
||
|
||
class TestSecretScrubber(TestCase): | ||
def setUp(self) -> None: | ||
self.scrubber = SecretScrubber() | ||
# these are not real. I randomly generated them | ||
# and separated them into two string to avoid any | ||
# scanners picking them up | ||
# used exrex: https://github.com/asciimoo/exrex | ||
self.aws_access_key_id = "L194ZYK14" + "K8XMRS9RIEE" | ||
self.aws_secret_access_key = "NJuU/e4plYJ" + "gc6ykqaykh6yXoQxYFmdO+RNJtYHV" | ||
self.graph_api_token = ( | ||
"EAA3XZGPxS4159hjkreOqw0cUmSLx6K6fzZvemMGnzSQrjLadM" | ||
"uhMS1ZeobDYIv4kBzGC5hU066Zj5ud0xNqqeNv3OhVJvYzqXUKit6Lj" | ||
) | ||
|
||
def test_scrub(self) -> None: | ||
test_message = f""" | ||
"CloudCredentialService": {{ | ||
"class": "fbpcs.pid.service.credential_service.simple_cloud_credential_service.SimpleCloudCredentialService", | ||
"constructor": {{ | ||
"access_key_id": "{self.aws_access_key_id}", | ||
"access_key_data": "{self.aws_secret_access_key}" | ||
}} | ||
}} | ||
access_token: {self.graph_api_token} | ||
""" | ||
|
||
expected_output = f""" | ||
"CloudCredentialService": {{ | ||
"class": "fbpcs.pid.service.credential_service.simple_cloud_credential_service.SimpleCloudCredentialService", | ||
"constructor": {{ | ||
"access_key_id": "{self.scrubber.REPLACEMENT_STR}", | ||
"access_key_data": "{self.scrubber.REPLACEMENT_STR}" | ||
}} | ||
}} | ||
access_token:{self.scrubber.REPLACEMENT_STR} | ||
""" | ||
|
||
scrub_summary = self.scrubber.scrub(test_message) | ||
|
||
self.assertEqual(scrub_summary.scrubbed_output, expected_output) | ||
self.assertEqual(scrub_summary.total_substitutions, 3) | ||
for c in scrub_summary.name_to_num_subs.values(): | ||
self.assertEqual(c, 1) |
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