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

[sec_scan][19] add tsh scan keys implementation #44220

Merged
merged 7 commits into from
Jul 25, 2024
Merged

Conversation

tigrato
Copy link
Contributor

@tigrato tigrato commented Jul 15, 2024

This PR introduces the required code to transverse a directory(es), finding all the SSH private keys and report them back to the cluster using the device security enclave as authentication mechanism.

This PR is part of https://github.com/gravitational/access-graph/issues/637.

@tigrato tigrato force-pushed the tigrato/ssh-keys-impl11 branch from 7602ff2 to f19b92a Compare July 17, 2024 12:04
@tigrato tigrato changed the base branch from master to tigrato/ssh-keys-impl13 July 17, 2024 12:05
@tigrato tigrato added backport/branch/v14 no-changelog Indicates that a PR does not require a changelog entry backport/branch/v15 backport/branch/v16 labels Jul 17, 2024
@tigrato tigrato marked this pull request as ready for review July 17, 2024 12:06
@github-actions github-actions bot added size/md tsh tsh - Teleport's command line tool for logging into nodes running Teleport. labels Jul 17, 2024
@github-actions github-actions bot requested review from codingllama and justinas July 17, 2024 12:07
Copy link
Contributor

@justinas justinas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are any tests planned? We could, for example, test the FS walking functionality using io/fs if that's not too much trouble.

tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Show resolved Hide resolved
tool/tsh/common/scan.go Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
@tigrato tigrato force-pushed the tigrato/ssh-keys-impl13 branch from 406f2ba to d59e543 Compare July 18, 2024 09:27
@tigrato tigrato requested review from justinas and codingllama July 18, 2024 12:29
@tigrato tigrato force-pushed the tigrato/ssh-keys-impl11 branch from f19b92a to 4644101 Compare July 18, 2024 12:33
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
tool/tsh/common/scan.go Outdated Show resolved Hide resolved
@tigrato tigrato force-pushed the tigrato/ssh-keys-impl13 branch from fd27c60 to a2c56e0 Compare July 18, 2024 15:17
lib/secretsscanner/scan/scan.go Outdated Show resolved Hide resolved
lib/secretsscanner/scan/scan.go Outdated Show resolved Hide resolved
lib/secretsscanner/scan/scan.go Outdated Show resolved Hide resolved
defer file.Close()

// read the first 150 bytes of the file to check if it's an OpenSSH private key.
var buf [150]byte
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an aside, why did you pick 150? Are we guaranteed no key will ever be smaller than that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect the key to be pem encoded and just the BEGIN + END are arround 100 bytes.
There is no key in pem encoded format that is bellow 150 bytes but I reduced it to the maximum BEGIN size which is 40

lib/secretsscanner/scan/scan.go Outdated Show resolved Hide resolved
lib/secretsscanner/scan/scan_test.go Outdated Show resolved Hide resolved
err := os.Mkdir(filepath.Join(dir, key.Name), os.ModePerm)
require.NoError(t, err)

filePath := filepath.Join(dir, key.Name, key.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why key.Name twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to ensure the walk function iterates through all directories and subdirectories

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a brief comment so it's clearer.

"github.com/google/uuid"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/ssh"
cryptosshtestdata "golang.org/x/crypto/ssh/testdata"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it wise to import test data from a third party package?

This seems like the kind of package that could easily change or disappear during a version update, leaving us broken.

It also seems like our test scenarios could silently shift without us realizing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I attempted to avoid it since Panther would flag a secret leak in this PR.

Anyway, I forked the ssh's test data private keys, retained the credits, and we can proceed with it.

lib/secretsscanner/scan/scan_test.go Outdated Show resolved Hide resolved
return expectedKeys
}

func writeEncryptedKeyWithoutPubFile(t *testing.T, dir string) []*accessgraphsecretsv1pb.PrivateKey {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this naturally happen on writeEncryptedKeys, by virtual of not all keys having "IncludesPublicKey: true"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IncludesPublicKey indicates that the passphrase-protected private key contains the un-encrypted public key in its headers. This allows for the extraction of the actual public key without needing to know the private key's passphrase.

In writeEncryptedKeys, I used the passphrase to decrypt the RSA private key and extract the public key to create a .pub file. Here, I am focusing on the scenario where the public key does not exist at all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant is that this is already covered by test keys that do not have "IncludesPublicKey=true", right?

@tigrato tigrato force-pushed the tigrato/ssh-keys-impl13 branch from a2c56e0 to e84919f Compare July 18, 2024 15:47
func (s *Scanner) findPrivateKeys(ctx context.Context, root, deviceID string) {
logger := s.log.With("dir", root)

err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the size of the Walk and the amount of I/O we'll cause, have you considered:

  1. Scanning hotspots first, like /Users/{user}/.ssh, and reporting those keys right away
  2. Walking with multiple goroutines
  3. Conversely to 2), walking cold paths at a much lower priority to not hog the disk
  4. Avoiding certain paths during walks (~/Library, node_modules, maybe even Git repositories?, etc)
  5. Possibly reducing the 150 bytes "pre-read" to the bare minimum to identify a PEM file?

Also, how long does a walk take in your machine?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my machine, it takes approximately 4 minutes to scan 1,842,916 items in the /Users/ directory, which includes the entire go mod cache storage.

The goal is to run these scripts during off-hours, such as nights or weekends. While we can parallelize the loop, I'm unsure if it's worth the effort given the relatively quick scan time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's... not as bad as I thought. Considering the size of ~/Library, .git directories, node_modules, etc, full of small files, each causing at least 150 (or 40?) bytes of I/O.

I still think it might be worth it to scan "hot spots" first, as I expect we'll hit some worst case scenarios eventually (slow machines with LOTS of files), but that could be a follow up (if you think it's worth doing).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth but not sure if it's worth now. I will revisit it once everything else lands

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, these are all suggestions for later. Feel free to resolve.

Base automatically changed from tigrato/ssh-keys-impl13 to master July 18, 2024 16:23
tigrato added 6 commits July 19, 2024 10:50
This PR introduces the required code to transverse a directory(es), finding all the SSH private keys and report them back to the cluster using the device security enclave as authentication mechanism.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>
@tigrato tigrato force-pushed the tigrato/ssh-keys-impl11 branch from 438ae9e to 35e68a1 Compare July 19, 2024 10:25
Copy link

@orca-security-us orca-security-us bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Failed Failed Secrets high 19   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca
🔑 The following Secrets have been detected in your pull request across all commits

⚠️ Please take action to mitigate the risk of the identified secrets by revoking them, and if already in use, updating all dependent systems

NAME FILE LINE NUM COMMIT
high Private Key ...testdata/ssh_keys.go 207 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 162 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 29 5a8c74f View in code
high Private Key ...data/invalid_keys.go 44 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 280 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 150 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 131 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 115 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 107 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 140 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 42 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 35 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 23 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 265 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 168 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 243 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 78 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 50 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 10 5a8c74f View in code

@public-teleport-github-review-bot

@tigrato - this PR will require admin approval to merge due to its size. Consider breaking it up into a series smaller changes.

lib/secretsscanner/scaner/scan.go Outdated Show resolved Hide resolved
lib/secretsscanner/scaner/scan.go Outdated Show resolved Hide resolved
require.NoError(t, err)

err = os.WriteFile(filepath.Join(dir, "invalid-key-invalid-header"), []byte(
`abcefg-----BEGIN OPENSSH PRIVATE KEY-----\n
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have confirmed that this errors, should we change our bytes.Contains check to bytes.HasPrefix? (Scanner.readFileIfSSHPrivateKey)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to avoid that because pem decode supports \n-----BEGIN OPENSSH PRIVATE KEY-----

Copy link
Contributor

@codingllama codingllama Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bytes.HasPrefix(bytes.TrimLeft(b, "\n "), "-----BEGIN OPENSSH PRIVATE KEY----")

or, if you'd rather not do that, leave a comment explaining why we do a Contains check instead of HasPrefix.

I would also add the \n-----BEGIN OPENSSH PRIVATE KEY----- to the scenarios.

lib/secretsscanner/reporter/env_test.go Show resolved Hide resolved
lib/secretsscanner/reporter/env_test.go Outdated Show resolved Hide resolved
lib/secretsscanner/reporter/report.go Outdated Show resolved Hide resolved
lib/secretsscanner/reporter/report.go Outdated Show resolved Hide resolved
Comment on lines +45 to +46
// disable TLS routing check for tests
t.Setenv(defaults.TLSRoutingConnUpgradeEnvVar, "false")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to set this? Is it for "secretsscannerclient.NewSecretsScannerServiceClient"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it includes a check for TLS routing, which we need to override because we don't run a full proxy API.

lib/secretsscanner/reporter/report_test.go Outdated Show resolved Hide resolved
lib/secretsscanner/reporter/report_test.go Outdated Show resolved Hide resolved
Copy link

@orca-security-us orca-security-us bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Failed Failed Secrets high 19   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca
🔑 The following Secrets have been detected in your pull request across all commits

⚠️ Please take action to mitigate the risk of the identified secrets by revoking them, and if already in use, updating all dependent systems

NAME FILE LINE NUM COMMIT
high Private Key ...testdata/ssh_keys.go 78 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 35 5a8c74f View in code
high Private Key ...data/invalid_keys.go 44 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 168 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 162 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 115 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 265 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 207 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 42 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 280 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 50 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 29 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 23 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 243 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 131 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 140 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 107 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 10 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 150 5a8c74f View in code

@tigrato tigrato force-pushed the tigrato/ssh-keys-impl11 branch from 4da0e1b to 7138ae2 Compare July 19, 2024 16:56
Copy link

@orca-security-us orca-security-us bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Failed Failed Secrets high 19   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca
🔑 The following Secrets have been detected in your pull request across all commits

⚠️ Please take action to mitigate the risk of the identified secrets by revoking them, and if already in use, updating all dependent systems

NAME FILE LINE NUM COMMIT
high Private Key ...testdata/ssh_keys.go 162 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 115 5a8c74f View in code
high Private Key ...data/invalid_keys.go 44 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 207 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 42 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 23 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 265 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 168 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 150 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 140 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 107 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 78 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 29 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 10 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 280 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 243 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 131 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 50 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 35 5a8c74f View in code

@tigrato tigrato force-pushed the tigrato/ssh-keys-impl11 branch from 7138ae2 to def568e Compare July 25, 2024 14:35
Copy link

@orca-security-us orca-security-us bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Failed Failed Secrets high 19   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca
🔑 The following Secrets have been detected in your pull request across all commits

⚠️ Please take action to mitigate the risk of the identified secrets by revoking them, and if already in use, updating all dependent systems

NAME FILE LINE NUM COMMIT
high Private Key ...testdata/ssh_keys.go 131 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 50 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 35 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 29 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 140 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 168 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 107 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 42 5a8c74f View in code
high Private Key ...data/invalid_keys.go 44 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 280 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 265 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 207 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 162 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 78 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 23 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 243 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 150 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 115 5a8c74f View in code
high Private Key ...testdata/ssh_keys.go 10 5a8c74f View in code

Copy link
Collaborator

@r0mant r0mant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bot.

@tigrato tigrato added this pull request to the merge queue Jul 25, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jul 25, 2024
@tigrato tigrato added this pull request to the merge queue Jul 25, 2024
Merged via the queue into master with commit 7bebfdc Jul 25, 2024
38 of 39 checks passed
@tigrato tigrato deleted the tigrato/ssh-keys-impl11 branch July 25, 2024 15:37
@public-teleport-github-review-bot

@tigrato See the table below for backport results.

Branch Result
branch/v14 Failed
branch/v15 Failed
branch/v16 Failed

tigrato added a commit that referenced this pull request Jul 30, 2024
* [sec_scan][19] add `tsh scan keys` implementation

This PR introduces the required code to transverse a directory(es), finding all the SSH private keys and report them back to the cluster using the device security enclave as authentication mechanism.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* handle code review

* fix message

* handle code review

* fork ssh private keys

* add skip dirs support

* handle code review

---------

Signed-off-by: Tiago Silva <[email protected]>
tigrato added a commit that referenced this pull request Jul 30, 2024
* [sec_scan][19] add `tsh scan keys` implementation

This PR introduces the required code to transverse a directory(es), finding all the SSH private keys and report them back to the cluster using the device security enclave as authentication mechanism.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* handle code review

* fix message

* handle code review

* fork ssh private keys

* add skip dirs support

* handle code review

---------

Signed-off-by: Tiago Silva <[email protected]>
github-merge-queue bot pushed a commit that referenced this pull request Aug 13, 2024
* Add the device assertion protos (#43804)

* Add the device assertion protos

* Update generated protos

* Add a client-side API to assert devices (#43890)

* Add a client-side API to assert devices

* Add a godoc to authnStreamAdapter

* Define server-side device assertion interfaces (#44036)

* Define server-side device assertion interfaces

* Update proto comments

* Update generated protos

* [sec_scan][1] Add `teleport.access_graph.v1.SecretsScannerService` (#43462)

This PR introduces the `teleport.access_graph.v1.SecretsScannerService`that will be used by Teleport SSH nodes to report `authorized_keys` and user's laptops to report secrets found on them.

The `ReportAuthorizedKeys` uses node's TLS certs signed by HostCA for authentication while `ReportSecrets` leverages the device trust credentials (requires that the device is enrolled) to report secrets without requiring valid user credentials.

handle Alan's feedback

* [sec_scan][2] expose `ssh_scan_enabled` in `AccessGraphConfig` response (#43467)

This PR exposes the configuration for nodes to be aware that they should report SSH Authorized keys to Teleport.

Part of gravitational/access-graph#637

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][3] add `PrivateKey`, `AuthorizedKey` and `Device` to Access Graph resources (#43468)

This PR extends the Access Graph resources to be able include the newly added `teleport.access_graph.v1.PrivateKey`,
`teleport.access_graph.v1.AuthorizedKey` and existing device trust information `teleport.devicetrust.v1.Device`.

Part of gravitational/access-graph#637

Signed-off-by: Tiago Silva <[email protected]>

* fix: fix `nextKey` values when using multiple prefixes (#43486)

This PR makes `generic.Service` correctly implementing `List*` functions when multiple key prefixes are defined

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][5] add secrets backend service (#43543)

* [sec_scan][5] add secrets backend service

This PR implements the backend service to support storing `authorized_keys` and `private_keys` into Teleport backend.

Part of gravitational/access-graph#637

Signed-off-by: Tiago Silva <[email protected]>

* handle feedback

* handle nits

---------

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][6] add device events (#43905)

This PR adds the ability to watch for events for `*devicepb.Device` objects.

Backend storage representation of  `devicepb.Device` is achieved using an internal representation that lives in `e/lib/devicetrust/storage` and whose logic is internal to the package.

To be able to expose the unmarshal logic necessary for events to work, this PR exposes a registration hook that `e/lib/devicetrust/storage` function must call during initialization to register the unmarshal function.

Part of gravitational/access-graph#637

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][7] add authorizedKeys and privateKeys events support (#43906)

This PR introduces the ability to watch for events related to `accessgraphsecretsv1pb.AuthorizedKey` and
`accessgraphsecretsv1pb.PrivateKey` objects.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][9] add `access_graph_settings` protobuf (#44010)

This PR adds the `clusterconfigpbv1.AccessGraphSettings` resource that will be used to control the secrets scanning definition of Teleport.

This resource will be a singleton and the only goal is to carry some settings related to access graph because on the cloud, users don't have access to fileconf.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][10] add `AccessGraphSettingsUpdate` audit event (#44011)

This PR adds the boilerplate code and proto definition for `AccessGraphSettingsUpdate` audit event.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][11] add `AccessGraphSettings` backend service (#44014)

This PR adds the backend service to be able to create, update and retrieve access graph configurations from Teleport backend.

This PR is part of gravitational/access-graph#637.

* [sec_scan][12] add cache and events support for `AccessGraphSettings` (#44016)

* [sec_scan][12] add cache and events support for `AccessGraphSettings`

This PR adds the cache and events support for the new resource `AccessGraphSettings`.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* add tests

---------

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][13] add `AccessGraphSettings` gRPC implementation (#44021)

This PR introduces the gRPC implementation for the CRUD operations related to `AccessGraphSettings`.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][14] create `AccessGraphSettings` on first auth init (#44032)

* [sec_scan][14] create `AccessGraphSettings` on first auth init

This PR adds a init script that sets `AccessGraphSettings` into Teleport backend when auth first inits and there is no `AccessGraphSettings`.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* remove iterations

---------

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][15] add support for edits to `AccessGraphSettings` via `tctl` (#44055)

This PR allows any cluster admin to edit `access_graph_settings` objects via `tctl`.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][16] add methods to store/retrieve device assertion functions (#44081)

This PR adds methods to store/retrieve functions defined by different teleport services.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][17] add `AssertDevice` to `FakeDeviceService` (#44159)

* [sec_scan][17] add `AssertDevice` to `FakeDeviceService`

This PR introduces a `AssertDevice` logic into `FakeDeviceService` to authenticate devices during unit tests using device trust credentials.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* simplify assert tests

* Update lib/devicetrust/assert/assert_test.go

Co-authored-by: Alan Parra <[email protected]>

---------

Signed-off-by: Tiago Silva <[email protected]>
Co-authored-by: Alan Parra <[email protected]>

* [sec_scan][20] add `ReportSecrets` forwarder to proxy's gRPC insecure server (#44324)

* [sec_scan][20] add `ReportSecrets` forwarder to proxy's gRPC insecure server

This PR implements a `ReportSecrets` forwarder from Proxy server to Auth server.
The goal is to allow clients to hit the proxy insecure gRPC server (credentialless)
and proxy will forward requests to the AuthServer on behalf of the client. This is required
because the client doesn't have valid credentials and it wasn't possible for it to reach auth server
via reversetunnel when the cluster uses `separate` mode.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* add comments

* move dial to lib/client/proxy/insecure

---------

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][19] add `tsh scan keys` implementation (#44220)

* [sec_scan][19] add `tsh scan keys` implementation

This PR introduces the required code to transverse a directory(es), finding all the SSH private keys and report them back to the cluster using the device security enclave as authentication mechanism.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* handle code review

* fix message

* handle code review

* fork ssh private keys

* add skip dirs support

* handle code review

---------

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][22] add authorized keys reporter (#44523)

* [sec_scan][22] add authorized keys reporter

This PR introduces a SSH authorized keys reporter that monitors `/etc/passwd` file and all users' authorized_keys files and reports the findings back to teleport.

Part of gravitational/access-graph#637

Signed-off-by: Tiago Silva <[email protected]>

* handle comments

* handle comments

---------

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][24] extract AuthorizedKey's comment and type (#44643)

This PR adds ability to extract the comment and key type from AuthorizedKeys files.

Signed-off-by: Tiago Silva <[email protected]>

* fix api module

* [sec_scan][27] add support for LDAP users and macOS (#45109)

* [sec_scan][27] add support for LDAP users and macOS

This PR extends support for authorized keys report for users managed by LDAP system and macOS targets.

It leverages `getpwent` to read the system database files and retrieve the user properties.
It doesn't use the `getpwent_r` because it's not available in macOS and because it's not (yet) standerdized

>   PLEASE NOTE: the `getpwent_r' function is not (yet) standardized.
>   The interface may change in later versions of this library.  But
>   the interface is designed following the principals used for the
>   other reentrant functions so the chances are good this is what the
>   POSIX people would choose.

Part of gravitational/access-graph#637

* handle comments

* handle comments 2

* add comment

---------

Signed-off-by: Tiago Silva <[email protected]>
Co-authored-by: Alan Parra <[email protected]>
github-merge-queue bot pushed a commit that referenced this pull request Aug 13, 2024
* Add the device assertion protos (#43804)

* Add the device assertion protos

* Update generated protos

* Add a client-side API to assert devices (#43890)

* Add a client-side API to assert devices

* Add a godoc to authnStreamAdapter

* Define server-side device assertion interfaces (#44036)

* Define server-side device assertion interfaces

* Update proto comments

* Update generated protos

* [sec_scan][1] Add `teleport.access_graph.v1.SecretsScannerService` (#43462)

This PR introduces the `teleport.access_graph.v1.SecretsScannerService`that will be used by Teleport SSH nodes to report `authorized_keys` and user's laptops to report secrets found on them.

The `ReportAuthorizedKeys` uses node's TLS certs signed by HostCA for authentication while `ReportSecrets` leverages the device trust credentials (requires that the device is enrolled) to report secrets without requiring valid user credentials.

handle Alan's feedback

* [sec_scan][2] expose `ssh_scan_enabled` in `AccessGraphConfig` response (#43467)

This PR exposes the configuration for nodes to be aware that they should report SSH Authorized keys to Teleport.

Part of gravitational/access-graph#637

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][3] add `PrivateKey`, `AuthorizedKey` and `Device` to Access Graph resources (#43468)

This PR extends the Access Graph resources to be able include the newly added `teleport.access_graph.v1.PrivateKey`,
`teleport.access_graph.v1.AuthorizedKey` and existing device trust information `teleport.devicetrust.v1.Device`.

Part of gravitational/access-graph#637

Signed-off-by: Tiago Silva <[email protected]>

* fix: fix `nextKey` values when using multiple prefixes (#43486)

This PR makes `generic.Service` correctly implementing `List*` functions when multiple key prefixes are defined

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][5] add secrets backend service (#43543)

* [sec_scan][5] add secrets backend service

This PR implements the backend service to support storing `authorized_keys` and `private_keys` into Teleport backend.

Part of gravitational/access-graph#637

Signed-off-by: Tiago Silva <[email protected]>

* handle feedback

* handle nits

---------

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][6] add device events (#43905)

This PR adds the ability to watch for events for `*devicepb.Device` objects.

Backend storage representation of  `devicepb.Device` is achieved using an internal representation that lives in `e/lib/devicetrust/storage` and whose logic is internal to the package.

To be able to expose the unmarshal logic necessary for events to work, this PR exposes a registration hook that `e/lib/devicetrust/storage` function must call during initialization to register the unmarshal function.

Part of gravitational/access-graph#637

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][7] add authorizedKeys and privateKeys events support (#43906)

This PR introduces the ability to watch for events related to `accessgraphsecretsv1pb.AuthorizedKey` and
`accessgraphsecretsv1pb.PrivateKey` objects.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][9] add `access_graph_settings` protobuf (#44010)

This PR adds the `clusterconfigpbv1.AccessGraphSettings` resource that will be used to control the secrets scanning definition of Teleport.

This resource will be a singleton and the only goal is to carry some settings related to access graph because on the cloud, users don't have access to fileconf.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][10] add `AccessGraphSettingsUpdate` audit event (#44011)

This PR adds the boilerplate code and proto definition for `AccessGraphSettingsUpdate` audit event.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][11] add `AccessGraphSettings` backend service (#44014)

This PR adds the backend service to be able to create, update and retrieve access graph configurations from Teleport backend.

This PR is part of gravitational/access-graph#637.

* [sec_scan][12] add cache and events support for `AccessGraphSettings` (#44016)

* [sec_scan][12] add cache and events support for `AccessGraphSettings`

This PR adds the cache and events support for the new resource `AccessGraphSettings`.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* add tests

---------

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][13] add `AccessGraphSettings` gRPC implementation (#44021)

This PR introduces the gRPC implementation for the CRUD operations related to `AccessGraphSettings`.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][14] create `AccessGraphSettings` on first auth init (#44032)

* [sec_scan][14] create `AccessGraphSettings` on first auth init

This PR adds a init script that sets `AccessGraphSettings` into Teleport backend when auth first inits and there is no `AccessGraphSettings`.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* remove iterations

---------

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][15] add support for edits to `AccessGraphSettings` via `tctl` (#44055)

This PR allows any cluster admin to edit `access_graph_settings` objects via `tctl`.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][16] add methods to store/retrieve device assertion functions (#44081)

This PR adds methods to store/retrieve functions defined by different teleport services.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][17] add `AssertDevice` to `FakeDeviceService` (#44159)

* [sec_scan][17] add `AssertDevice` to `FakeDeviceService`

This PR introduces a `AssertDevice` logic into `FakeDeviceService` to authenticate devices during unit tests using device trust credentials.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* simplify assert tests

* Update lib/devicetrust/assert/assert_test.go

Co-authored-by: Alan Parra <[email protected]>

---------

Signed-off-by: Tiago Silva <[email protected]>
Co-authored-by: Alan Parra <[email protected]>

* [sec_scan][20] add `ReportSecrets` forwarder to proxy's gRPC insecure server (#44324)

* [sec_scan][20] add `ReportSecrets` forwarder to proxy's gRPC insecure server

This PR implements a `ReportSecrets` forwarder from Proxy server to Auth server.
The goal is to allow clients to hit the proxy insecure gRPC server (credentialless)
and proxy will forward requests to the AuthServer on behalf of the client. This is required
because the client doesn't have valid credentials and it wasn't possible for it to reach auth server
via reversetunnel when the cluster uses `separate` mode.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* add comments

* move dial to lib/client/proxy/insecure

---------

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][19] add `tsh scan keys` implementation (#44220)

* [sec_scan][19] add `tsh scan keys` implementation

This PR introduces the required code to transverse a directory(es), finding all the SSH private keys and report them back to the cluster using the device security enclave as authentication mechanism.

This PR is part of gravitational/access-graph#637.

Signed-off-by: Tiago Silva <[email protected]>

* handle code review

* fix message

* handle code review

* fork ssh private keys

* add skip dirs support

* handle code review

---------

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][22] add authorized keys reporter (#44523)

* [sec_scan][22] add authorized keys reporter

This PR introduces a SSH authorized keys reporter that monitors `/etc/passwd` file and all users' authorized_keys files and reports the findings back to teleport.

Part of gravitational/access-graph#637

Signed-off-by: Tiago Silva <[email protected]>

* handle comments

* handle comments

---------

Signed-off-by: Tiago Silva <[email protected]>

* [sec_scan][24] extract AuthorizedKey's comment and type (#44643)

This PR adds ability to extract the comment and key type from AuthorizedKeys files.

Signed-off-by: Tiago Silva <[email protected]>

* update gomod

* [sec_scan][27] add support for LDAP users and macOS (#45109)

* [sec_scan][27] add support for LDAP users and macOS

This PR extends support for authorized keys report for users managed by LDAP system and macOS targets.

It leverages `getpwent` to read the system database files and retrieve the user properties.
It doesn't use the `getpwent_r` because it's not available in macOS and because it's not (yet) standerdized

>   PLEASE NOTE: the `getpwent_r' function is not (yet) standardized.
>   The interface may change in later versions of this library.  But
>   the interface is designed following the principals used for the
>   other reentrant functions so the chances are good this is what the
>   POSIX people would choose.

Part of gravitational/access-graph#637

* handle comments

* handle comments 2

* add comment

---------

Signed-off-by: Tiago Silva <[email protected]>
Co-authored-by: Alan Parra <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/branch/v14 backport/branch/v15 backport/branch/v16 no-changelog Indicates that a PR does not require a changelog entry size/md tsh tsh - Teleport's command line tool for logging into nodes running Teleport.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants