Commit Graph
269 Commits
Author SHA1 Message Date
rosstimothy c2fc9a9375 Add idle connection timeouts to http clients and servers (#22885)
Sets `http.Server.IdleTimeout` and `http.Client.IdleConnTimeout`
on clients and servers which didn't have them set. A default of
360s was chosen to be on par with the default of an NLB without
being identical.

This was added as another safety measure to prevent leaking any
idle connections indefinitely as seen in #22757.

`apidefaults.DefaultDialTimeout` was also renamed to
`apidefaults.DefaultIOTimeout` to better reflect its usage.
2023-03-10 16:47:06 +00:00
rosstimothy 93d7831ced Use btree.BTreeG directly in memory backend (#22282)
`btree.BTree` has been a backward compatability wrapper over a
`btree.BTreeG` since `github.com/google/btree@v1.1.0`. This converts
all usages to the new generic variant.
2023-02-27 17:54:44 +00:00
Michael Wilsonandrosstimothy d530f33b5c Create a generic local backend service. (#21078)
* Create a generic local backend service.

Many of the local backend services have a very similar pattern in terms of how
objects are retrieved and stored. This PR introduces a generic backend which
implements backend retrieval and storage of objects and migrates the SAML
IdP service provider service to it. More services could easily be migrated
to this generic service implementation.

* Feedback, add in generic tests.

* Tim's feedback, added in a test for updating a resource that doesn't exist.

* Tim's feedback.

* Apply suggestions from code review

Co-authored-by: rosstimothy <39066650+rosstimothy@users.noreply.github.com>

* Edoardo's feedback.

* Added in UpdateAndSwap.

* Edoardo's feedback, Tim's feedback.

* Use a default page size if the page limit is not specified.

* More of Edoardo's feedback.

* Propagate error instead of panic.

---------

Co-authored-by: rosstimothy <39066650+rosstimothy@users.noreply.github.com>
2023-02-24 15:59:16 +00:00
rosstimothy 6e9a3cfcc9 Reduce etcd requests performed by a KeepAlive (#21892)
When performing a KeepAlive the key was first retrieved from
etcd to verify that it existed before updating the item. However,
a Put operation with the `ignore_value` set will return a not
found error if the key does not exist
(https://etcd.io/docs/v3.4/learning/api/#put). So we can safely
remove the get and rely on etcd to prevent updating a key that
does not exist.

Also converts `concurrentqueue.Queue` to be generic over the given
Input and Output of the worker function.
2023-02-16 15:27:39 +00:00
Michael Wilson 763410ca70 Lock access to mock Firestore Server reqs. (#21742)
The reqs member of the mock Firestore Server used in the `TestDeleteDocuments`
test was occasionally causing a data race. Accesses to this field have now
been wrapped in a lock.
2023-02-13 19:28:56 +00:00
Joel 2402509f33 Update dependencies blocked by the now-updated firestore version in Teleport + nits (#21653)
* update dep blocked by firestore

* range syntax

* bumpgenproto locally too, due to it being required

* sorry, what

* please

* tidy up
2023-02-10 18:14:08 +00:00
Joelandrosstimothy f93d401df2 Update Firestore and held-back dependencies (#21190)
* update dep

* implicit aggregate

* fix kind check

* remove cring address manip

* correct import

* invert if

* fix rename

---------

Co-authored-by: rosstimothy <39066650+rosstimothy@users.noreply.github.com>
2023-02-10 14:20:15 +00:00
rosstimothy c6d8fca73f Cache etcd lease ttl (#21481)
Since #16636 etcd leases have been bucketed, yet when watching events
we still determine the ttl for each particular item in `fromEvent`
by retrieving the ttl from etcd. To reduce the number of ttl requests
sent to etcd we now cache the ttl per lease id.

Closes #21380
2023-02-08 20:35:04 +00:00
Jakub Nyckowski 79ebdc89c8 Bump golangci-lint to v1.51.1 (#21440) 2023-02-08 00:44:08 +00:00
Noah Stride 476a4a7e82 context.TODO() tidying (#21288)
* Use `ctx` rather than `context.TODO()` where available

* Further propagation of context

* Use `context.Background()` instead of `context.TODO()` where appropriate in tests

* Further remove `context.TODO` from tests

* Appease linter on parameter order
2023-02-07 09:01:35 +00:00
Tiago Silva 3a5c1e2639 Adds a post-delete hook to delete the kube-agent state secrets (#20043)
This PR adds a `post-delete` hook that cleans the state secret that each Pod creates when it receives the identity from the Teleport Auth Server.

Fixes #20035
Fixes #18854
2023-01-12 22:27:30 +00:00
Forrest Marshall ecef3465b6 instance heartbeats 2022-12-30 12:09:11 -08:00
Steven Martin e1ebc332f8 spell fixes (#19419) 2022-12-16 19:01:28 +00:00
Lisa Kim 248198fe34 Add new prefixes to the "sensitiveBackendPrefixes" list (#19287) 2022-12-14 16:52:52 +00:00
Vitor Enes 0403a4eebb Improve error message if dialing etcd nodes times out (#18789)
If all etcd nodes are unreachable, Teleport exits with the following
message:
```
ERROR: initialization failed
context deadline exceeded
```

With this commit we get instead:
```
ERROR: timed out dialing etcd endpoints: [https://127.0.0.1:2381 https://127.0.0.1:12381]
initialization failed
context deadline exceeded
```

Note that if only some of the etcd nodes are unreachable, the etcd
client already provides a useful error message:
```
ERROR: initialization failed
failed to dial endpoint https://127.0.0.1:12381 with maintenance client: context deadline exceeded
```
2022-11-28 18:40:24 +00:00
Roman TkachenkoandVictor Sokolov 3fd1cca5b2 Github Actions workflow (#18617)
Signed-off-by: Roman Tkachenko <roman@goteleport.com>
Co-authored-by: Victor Sokolov <gzigzigzeo@gmail.com>
2022-11-22 16:55:26 -05:00
Zac Bergquist da7680ad0b Use x/exp/slices instead of home grown utilities (#18524)
We were inconsistent throughout the codebase and would sometimes
use the slices package and other times use our own equivalents
in api/.

This removes our versions in favor of the golang.org/x package that
does the same, which has the added benefit of reducing the surface
area of the public API module.

Note: despite existing uses of the slices package, for some reason
it didn't show up in go.mod or go.sum. Fixed that too.
2022-11-17 15:25:46 +00:00
Jakub NyckowskiandAlan Parra bea2e89df5 Run GCI as make fix-imports (#17956)
Co-authored-by: Alan Parra <alan.parra@goteleport.com>
2022-11-04 15:46:46 +00:00
Jakub Nyckowski 0ee91f6c37 Enable GCI linter (#17894) 2022-10-28 20:20:28 +00:00
Edoardo Spadolini 236b1b2f3c Remove the SQL backend (#17057)
* Revert "Azure AD authentication for the Postgres backend (#15757)"

This reverts commit 33c6d82dc3.

* Revert "SQL Backend (#11048)"

This reverts commit 06fef2abf1.

* Remove Postgres backend from the docs

* Remove the Postgres backend from the testplan
2022-10-07 10:40:44 +02:00
Zac Bergquist 1a62376b78 Fix uses of require in goroutines (#16953)
The require checks from testify call (*testing.T).Fail, which should
only be called from the test goroutine.
2022-10-05 19:14:30 +00:00
rosstimothy 1750b79951 Stop using etcd serializable mode (#16941)
From the etcd docs:

> To obtain lower latencies and higher throughput for read requests
> clients can configure a request’s consistency mode to serializable,
> which may access stale data with respect to quorum, but removes the
> performance penalty of linearized accesses' reliance on live consensus.

By leveraging serializable mode it is entirely possible to retrieve
stale data. This means that a Create followed by a Get could result
in a NotFound error, which results in issues like #13276. While this
improves latency it breaks the consistency guarantees required by
Teleport.

To ensure that our consistency requirements are met all etcd operations
are converted to use the default linearized mode by removing the
`WithSerializable` option.

Closes #13276.
2022-10-05 02:09:29 +00:00
Forrest Marshall ad4be5ae44 bucket etcd leases 2022-09-27 15:01:32 -07:00
Brian Joerger 4c0a6ff5b1 tsh PIV login integration (#15335)
* Add Yubikey PrivateKey implementation for use by Teleport clients.

  - Add yubikey login logic, reusing previously stored private keys.

  - Fix identity file decoding with PIV keys, which sign ecdsa certificates.

  - Add libpcsclite-dev pre-req for building on linux.

  - Remove unnecessary keys.Signer interface and move its functionality to keys.PrivateKey.

  - Move retry and jitter utils to new api/utils/retryutils package.
2022-09-23 19:44:10 +00:00
Roman Tkachenko 9d04ee27c2 Fix CA pool loading for etcd backend (#16484) 2022-09-20 22:03:07 +00:00
Alan Parra a75fcc21d8 Update golangci-lint to 1.49.0 (#16507)
Update metalinter, fix a few lint warnings and replace deprecated linters.

`deadcode`, `structcheck` and `varcheck` are abandoned and now replaced by [`unused`][1].

Since 1.19, `go fmt` reformats godocs according to https://go.dev/doc/comment. I've done a bulk-reformatting of the codebase to keep the linter happy. Backporting is mostly harmless (the exception being `lib/services/role_test.go`, that for some reason breaks the _old_ linter using the new format).

[1]: https://golangci-lint.run/usage/linters/

* Bump golangci-lint version
* Replace abandoned linters
* Fix bodyclose on lib/auth/github.com
* Fix bodyclose on lib/kube/proxy/streamproto/proto_test.go
* Fix bodyclose on lib/srv/alpnproxy/proxy_test.go
* Fix bodyclose on lib/web/conn_upgrade_test.go
* Silence staticcheck on lib/kube/proxy/forwarder_test.go
* Silence staticcheck on lib/utils/certs_test.go
* Address BuildNameToCertificate deprecation warnings
* Run `go fmt ./...`
* Run `go fmt ./...` on api/
* Ignore formatting in role_test.go
* Remove redundant initializers in lib/srv/uacc/
* Update e/
2022-09-19 22:38:59 +00:00
Edoardo Spadolini 33c6d82dc3 Azure AD authentication for the Postgres backend (#15757)
* Add Username to sqlbk and don't leak connConfigs

* Azure AD authentication for sqlbk/Postgres

* Add a Postgres Config test

* Cache Azure tokens, document azureBeforeConnect

* Move the config test to sqlbk

* go mod tidy

* go get azcore azidentity
2022-09-15 17:26:12 +00:00
rosstimothy b74acc43fa Increase dynamo get limit (#16076)
Large clusters which experience a lot of churn can have a backend
populated with expired items yet to be deleted due to dynamo ttl
limitations. Increasing the limit as a stop gap for now until a
better solution can be put in place.
2022-09-02 17:16:32 +00:00
Joel 74281c5b08 Fix firestore indexes (#15443) 2022-08-25 15:52:19 +00:00
Vitor Enes f7634d1f72 Fix some typos in lib/ (#15556) 2022-08-23 07:34:36 +00:00
Vitor Enes e66ead445d Remove unused vars in buffer.go (#15553) 2022-08-22 19:22:06 +00:00
Joel 6caba42ec1 Add support for IdP-Initiated SAML2 login (#13924) 2022-08-22 18:27:44 +00:00
Vitor Enes ee3a2e91b7 Remove unused vars in dynamodbbk.go (#15554) 2022-08-17 12:18:17 +00:00
Vitor Enes e6dacedfa0 Add TurnOnTimeToLive and TurnOnStreams functions to dynamo/configure.go (#15557) 2022-08-17 10:50:18 +00:00
rosstimothy c469a34994 Move prometheus collectors from utils to metrics (#15288) 2022-08-09 17:35:19 +00:00
Zac Bergquist d1c6b0618e Fix lint warnings (#15312)
Mostly duplicated imports and redundant types in struct literals.
2022-08-08 20:20:29 +00:00
Tiago Silva 037daad083 Introduce dedicated server type for Kubernetes resources (#14389)
## What

First part of the Kubernetes [Discovery RFD](https://github.com/gravitational/teleport/pull/13376/) to introduce a Kubernetes server per cluster. 

This PR introduces a separate Kubernetes server that uses the already introduced `KubernetesClusterV3`. 

## Compatibility

In previous versions, Kubernetes Clusters were part of regular `ServerV2` resource and this refactoring deprecates the `ServerV2` usage but keeps them for compatibility with previous version.

Everything is backward compatible, so v10 kubernetes agents and trusted clusters can connect fine.

## Next steps

Once this is merged, a new PR will introduce dynamic registration for Kubernetes Clusters discovered through EKS Discovery.
2022-08-04 14:21:11 +00:00
Edoardo Spadolini 78cab266a2 Don't reset eventID to 0 when out of events in the Postgres backend (#15160) 2022-08-03 17:41:34 +00:00
Tiago Silva 40abd14635 Kubernetes Secret storage for Agent's Identity (#13942)
This MR introduces Kubernetes Secret storage for Teleport Kubernetes Agent state.

When running Teleport Agent inside a Kubernetes cluster where TELEPORT_REPLICA_NAME and KUBE_NAMESPACE exist, the agent will store identities and states in a Kubernetes Secret instead of local storage.

Otherwise, if the above environment variables are not present or not in Kubernetes, the agent persists state in SQLite.

Notes:

Fresh installs will enable the Kubernetes Secret Storage by default.
If PV storage is configured, the agent will store its identity in a Secret and use the PV for non-identity objects.
If the operator is upgrading from an existing release that didn't support Secret storage, the Helm chart will convert the objects to enable Kube Secret Storage. If the current object is a Deployment, the Helm chart installs a new Statefulset side by side and will trigger a Helm hook that is responsible for deleting the Deployment once Statefulset pods are healthy. Otherwise, if upgrading from a Statefulset the agent will read the credentials stored in PV and move them into Kube Secret.

Implementation of #12958
Related to #5585
2022-07-29 13:21:51 +00:00
Marco André Dinis 24fcadc375 Firestore: Err Not Found if doc was already deleted (#14932)
Most of the backends implementation we have return an error when the
item does not exist.
This allow us to make stronger assumptions when dealing with possible
concurrent issues.

*dynamo* ✅
https://github.com/gravitational/teleport/blob/0c48cefef211ff5c6841f025cebfb8eb1407e04e/lib/backend/dynamo/dynamodbbk.go#L855
AWS docs state that we can receive a ResourceNotFoundException if the item does not exist
https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html#API_DeleteItem_Errors

*InMemory* ✅
https://github.com/gravitational/teleport/blob/02b4f8575f5f0bc305a37c89b58e4469c2662921/lib/backend/memory/memory.go#L277
Protected by a lock

*SQLite* ✅
https://github.com/gravitational/teleport/blob/e9fb1e84e25546e1728e71c750a0ae2a5ddaf30c/lib/backend/lite/lite.go#L822

*etcd* ✅
https://github.com/gravitational/teleport/blob/02b4f8575f5f0bc305a37c89b58e4469c2662921/lib/backend/etcdbk/etcd.go#L717
It checks if the total deleted is 0, if so, returns Err NotFound

Firestore does not give us such guarantee.

```
// Delete deletes the document. If the document doesn't exist, it does nothing
// and returns no error.
```
Between the Get and Delete calls we can have another execution unit
doing a Delete. We would not receive an error if that happens.

We can add fix this by passing a PreCondition to the Delete call:
firestore.Exists.
This ensures we receive an error if, at the deletion moment, the doc was
deleted beforehand.

It's hard to do a proper test that validates we are safe from
concurrency issues, because we must keep the Get call in order to call
the `doc.Delete()`
I did validate this with a dummy project locally.
2022-07-28 07:30:54 +00:00
Russell Jones d65cacc64e Refactor tests under backend package.
Refactored all tests under "lib/backend" to use testify instead
of gocheck.
2022-07-22 11:37:11 -07:00
rosstimothy 0c48cefef2 Add dynamodb metrics (#14518)
* Add dynamodb metrics

Adds promethues counters for dynamo api requests, requests failed,
and latency of requests. Each counter is labeled with the table type,
either backend or events, and the operation being requested.
2022-07-21 13:31:50 -04:00
Noah Stride 02b4f8575f Configure linter to catch British 🇬🇧 spellings 🇺🇸 🦅 📖 (#14363)
* configure golangci-lint misspell to check for anglicized spellings

* Americanize spellings

* fix aws constant value with british spelling 🇬🇧

* update api types with americanized spellings

* use american spellings .cloudbuild/scripts
2022-07-14 10:51:23 +00:00
Joel aaa6ea73dd Correctly handle Firestore pagination with DocumentID cursors (#13017) 2022-06-22 16:48:21 +00:00
Michael McAllister 015e11bc85 Log initilization after CheckAndSetDefaults (#13090)
Super minor, but in this case we log that we're initializing the backend
_prior_ to cfg.CheckAndSetDefaults which means that if we are indeed
using the default the following will be printed:

> {"caller":"dynamo/dynamodbbk.go:206","component":"dynamodb","level":"info","message":"Initializing
backend. Table: \"example-table\", poll streams every
0s.","timestamp":"2022-06-01T23:49:49Z"}

Which is misleading as it's not polling the stream every "0s", it's
polling every 1s as per backend.DefaultPollStreamPeriod

Signed-off-by: Michael McAllister <michael.mcallister@goteleport.com>
2022-06-20 06:39:42 +00:00
rosstimothy 652e089d3d Manually instrument backend.Backend (#13268)
Adds a `trace.Tracer` to the `backend.Reporter`
wrapper so that all `bakend.Backend` implementations
can be traced. Further instrumentation of each specific
backend will be added at a later date to see how long
each sql query, or call to dynamo/etcd took within
each backend operation.

#12241
2022-06-11 16:09:42 +00:00
Jim Bishopp b96cff3acf Fix user mismatch in postgres backend (#12467)
Always set the user field of the database connection string to the same
user defined in the CommonName field of the client certificate.

The pgx library always includes a user field in the connection string
and defaults to $USER. The database connection will fail when $USER is
not the same as the value defined in the CommonName field of the client
certificate.
2022-05-10 16:41:05 +00:00
Andrew LeFevre cc27d91a45 add ExactKey function to create absolute storage paths
Storage paths returned by Key do not contain a trailing slash, which will
cause range matching to match on paths that contain the original path as
a prefix. By using ExactKey, range matching will only match sub-paths.

Fixes #5188.
2022-05-06 09:49:38 -04:00
Joel e9fb1e84e2 Harden SQLite permissions (#12096) 2022-05-02 14:12:45 +00:00
rosstimothy b543b997bb Respect Firestore commit write limits (#12111)
Firestore only allows 500 writes per commit. Prior to this change
DeleteRange or purgeExpiredDocuments could result in deleting
more than 500 documents and would result in an error. To comply with
the limits both DeletRange and purgeExpiredDocuments now call
deleteDocuments which properly handles limiting a batch to 500 writes
and commiting multiple times until all documents have been removed.

Fixes #12007
2022-04-21 10:25:14 -04:00