Skip to content

Commit

Permalink
fully refactor out auth.AccessPoint and auth.ReadAccessPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rosstimothy committed Oct 27, 2021
1 parent 6ff3844 commit f71301e
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 310 deletions.
462 changes: 225 additions & 237 deletions lib/auth/api.go

Large diffs are not rendered by default.

26 changes: 12 additions & 14 deletions lib/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2688,32 +2688,32 @@ func (a *Server) GetToken(ctx context.Context, token string) (types.ProvisionTok
return a.GetCache().GetToken(ctx, token)
}

// GetRoles is a part of auth.AccessPoint implementation
// GetRoles returns roles from the cache
func (a *Server) GetRoles(ctx context.Context) ([]types.Role, error) {
return a.GetCache().GetRoles(ctx)
}

// GetRole is a part of auth.AccessPoint implementation
// GetRole returns a role from the cache
func (a *Server) GetRole(ctx context.Context, name string) (types.Role, error) {
return a.GetCache().GetRole(ctx, name)
}

// GetNamespace returns namespace
// GetNamespace returns a namespace from the cache
func (a *Server) GetNamespace(name string) (*types.Namespace, error) {
return a.GetCache().GetNamespace(name)
}

// GetNamespaces is a part of auth.AccessPoint implementation
// GetNamespaces returns namespaces from the cache
func (a *Server) GetNamespaces() ([]types.Namespace, error) {
return a.GetCache().GetNamespaces()
}

// GetNodes is a part of auth.AccessPoint implementation
// GetNodes returns nodes from the cache
func (a *Server) GetNodes(ctx context.Context, namespace string, opts ...services.MarshalOption) ([]types.Server, error) {
return a.GetCache().GetNodes(ctx, namespace, opts...)
}

// ListNodes is a part of auth.AccessPoint implementation
// ListNodes lists nodes from the cache
func (a *Server) ListNodes(ctx context.Context, req proto.ListNodesRequest) ([]types.Server, string, error) {
return a.GetCache().ListNodes(ctx, req)
}
Expand Down Expand Up @@ -2744,34 +2744,32 @@ func (a *Server) IterateNodePages(ctx context.Context, req proto.ListNodesReques
}
}

// GetReverseTunnels is a part of auth.AccessPoint implementation
// GetReverseTunnels returns reverse tunnels from the cache
func (a *Server) GetReverseTunnels(opts ...services.MarshalOption) ([]types.ReverseTunnel, error) {
return a.GetCache().GetReverseTunnels(opts...)
}

// GetProxies is a part of auth.AccessPoint implementation
// GetProxies returns proxies from the cache
func (a *Server) GetProxies() ([]types.Server, error) {
return a.GetCache().GetProxies()
}

// GetUser is a part of auth.AccessPoint implementation.
// GetUser returns a user from the cache
func (a *Server) GetUser(name string, withSecrets bool) (user types.User, err error) {
return a.GetCache().GetUser(name, withSecrets)
}

// GetUsers is a part of auth.AccessPoint implementation
// GetUsers returns users from the cache
func (a *Server) GetUsers(withSecrets bool) (users []types.User, err error) {
return a.GetCache().GetUsers(withSecrets)
}

// GetTunnelConnections is a part of auth.AccessPoint implementation
// GetTunnelConnections are not using recent cache as they are designed
// to be called periodically and always return fresh data
func (a *Server) GetTunnelConnections(clusterName string, opts ...services.MarshalOption) ([]types.TunnelConnection, error) {
return a.GetCache().GetTunnelConnections(clusterName, opts...)
}

// GetAllTunnelConnections is a part of auth.AccessPoint implementation
// GetAllTunnelConnections are not using recent cache, as they are designed
// to be called periodically and always return fresh data
func (a *Server) GetAllTunnelConnections(opts ...services.MarshalOption) (conns []types.TunnelConnection, err error) {
Expand Down Expand Up @@ -2814,12 +2812,12 @@ func (a *Server) modeStreamer(ctx context.Context) (events.Streamer, error) {
return a.streamer, nil
}

// GetAppServers is a part of the auth.AccessPoint implementation.
// GetAppServers returns app servers from the cache
func (a *Server) GetAppServers(ctx context.Context, namespace string, opts ...services.MarshalOption) ([]types.Server, error) {
return a.GetCache().GetAppServers(ctx, namespace, opts...)
}

// GetAppSession is a part of the auth.AccessPoint implementation.
// GetAppSession returns app sessions from the cache
func (a *Server) GetAppSession(ctx context.Context, req types.GetAppSessionRequest) (types.WebSession, error) {
return a.GetCache().GetAppSession(ctx, req)
}
Expand Down
30 changes: 13 additions & 17 deletions lib/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func ForWindowsDesktop(cfg Config) Config {
// for cache
type SetupConfigFn func(c Config) Config

// Cache implements auth.AccessPoint interface and remembers
// Cache implements auth.Cache interface and remembers
// the previously returned upstream value for each API call.
//
// This which can be used if the upstream AccessPoint goes offline
Expand Down Expand Up @@ -1196,7 +1196,7 @@ func (c *Cache) GetClusterName(opts ...services.MarshalOption) (types.ClusterNam
return rg.clusterConfig.GetClusterName(opts...)
}

// GetRoles is a part of auth.AccessPoint implementation
// GetRoles is a part of auth.Cache implementation
func (c *Cache) GetRoles(ctx context.Context) ([]types.Role, error) {
rg, err := c.read()
if err != nil {
Expand All @@ -1206,7 +1206,7 @@ func (c *Cache) GetRoles(ctx context.Context) ([]types.Role, error) {
return rg.access.GetRoles(ctx)
}

// GetRole is a part of auth.AccessPoint implementation
// GetRole is a part of auth.Cache implementation
func (c *Cache) GetRole(ctx context.Context, name string) (types.Role, error) {
rg, err := c.read()
if err != nil {
Expand Down Expand Up @@ -1236,7 +1236,7 @@ func (c *Cache) GetNamespace(name string) (*types.Namespace, error) {
return rg.presence.GetNamespace(name)
}

// GetNamespaces is a part of auth.AccessPoint implementation
// GetNamespaces is a part of auth.Cache implementation
func (c *Cache) GetNamespaces() ([]types.Namespace, error) {
rg, err := c.read()
if err != nil {
Expand All @@ -1262,7 +1262,7 @@ type getNodesCacheKey struct {

var _ map[getNodesCacheKey]struct{} // compile-time hashability check

// GetNodes is a part of auth.AccessPoint implementation
// GetNodes is a part of auth.Cache implementation
func (c *Cache) GetNodes(ctx context.Context, namespace string, opts ...services.MarshalOption) ([]types.Server, error) {
rg, err := c.read()
if err != nil {
Expand Down Expand Up @@ -1294,7 +1294,7 @@ func (c *Cache) GetNodes(ctx context.Context, namespace string, opts ...services
return rg.presence.GetNodes(ctx, namespace, opts...)
}

// ListNodes is a part of auth.AccessPoint implementation
// ListNodes is a part of auth.Cache implementation
func (c *Cache) ListNodes(ctx context.Context, req proto.ListNodesRequest) ([]types.Server, string, error) {
// NOTE: we "fake" the ListNodes API here in order to take advantage of TTL-based caching of
// the GetNodes endpoint, since performing TTL-based caching on a paginated endpoint is nightmarish.
Expand Down Expand Up @@ -1352,7 +1352,7 @@ func (c *Cache) GetAuthServers() ([]types.Server, error) {
return rg.presence.GetAuthServers()
}

// GetReverseTunnels is a part of auth.AccessPoint implementation
// GetReverseTunnels is a part of auth.Cache implementation
func (c *Cache) GetReverseTunnels(opts ...services.MarshalOption) ([]types.ReverseTunnel, error) {
rg, err := c.read()
if err != nil {
Expand All @@ -1362,7 +1362,7 @@ func (c *Cache) GetReverseTunnels(opts ...services.MarshalOption) ([]types.Rever
return rg.presence.GetReverseTunnels(opts...)
}

// GetProxies is a part of auth.AccessPoint implementation
// GetProxies is a part of auth.Cache implementation
func (c *Cache) GetProxies() ([]types.Server, error) {
rg, err := c.read()
if err != nil {
Expand Down Expand Up @@ -1430,7 +1430,7 @@ func (c *Cache) GetRemoteCluster(clusterName string) (types.RemoteCluster, error
return rg.presence.GetRemoteCluster(clusterName)
}

// GetUser is a part of auth.AccessPoint implementation.
// GetUser is a part of auth.Cache implementation.
func (c *Cache) GetUser(name string, withSecrets bool) (user types.User, err error) {
if withSecrets { // cache never tracks user secrets
return c.Config.Users.GetUser(name, withSecrets)
Expand All @@ -1454,7 +1454,7 @@ func (c *Cache) GetUser(name string, withSecrets bool) (user types.User, err err
return user, trace.Wrap(err)
}

// GetUsers is a part of auth.AccessPoint implementation
// GetUsers is a part of auth.Cache implementation
func (c *Cache) GetUsers(withSecrets bool) (users []types.User, err error) {
if withSecrets { // cache never tracks user secrets
return c.Users.GetUsers(withSecrets)
Expand All @@ -1467,9 +1467,7 @@ func (c *Cache) GetUsers(withSecrets bool) (users []types.User, err error) {
return rg.users.GetUsers(withSecrets)
}

// GetTunnelConnections is a part of auth.AccessPoint implementation
// GetTunnelConnections are not using recent cache as they are designed
// to be called periodically and always return fresh data
// GetTunnelConnections is a part of auth.Cache implementation
func (c *Cache) GetTunnelConnections(clusterName string, opts ...services.MarshalOption) ([]types.TunnelConnection, error) {
rg, err := c.read()
if err != nil {
Expand All @@ -1479,9 +1477,7 @@ func (c *Cache) GetTunnelConnections(clusterName string, opts ...services.Marsha
return rg.presence.GetTunnelConnections(clusterName, opts...)
}

// GetAllTunnelConnections is a part of auth.AccessPoint implementation
// GetAllTunnelConnections are not using recent cache, as they are designed
// to be called periodically and always return fresh data
// GetAllTunnelConnections is a part of auth.Cache implementation
func (c *Cache) GetAllTunnelConnections(opts ...services.MarshalOption) (conns []types.TunnelConnection, err error) {
rg, err := c.read()
if err != nil {
Expand All @@ -1491,7 +1487,7 @@ func (c *Cache) GetAllTunnelConnections(opts ...services.MarshalOption) (conns [
return rg.presence.GetAllTunnelConnections(opts...)
}

// GetKubeServices is a part of auth.AccessPoint implementation
// GetKubeServices is a part of auth.Cache implementation
func (c *Cache) GetKubeServices(ctx context.Context) ([]types.Server, error) {
rg, err := c.read()
if err != nil {
Expand Down
12 changes: 0 additions & 12 deletions lib/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1167,18 +1167,6 @@ func (tc *TeleportClient) LoadKeyForClusterWithReissue(ctx context.Context, clus
return nil
}

// accessPoint returns access point based on the cache policy
func (tc *TeleportClient) accessPoint(clt auth.AccessPoint, proxyHostPort string, clusterName string) (auth.AccessPoint, error) {
// If no caching policy was set or on Windows (where Teleport does not
// support file locking at the moment), return direct access to the access
// point.
if tc.CachePolicy == nil || runtime.GOOS == constants.WindowsOS {
log.Debugf("not using caching access point")
return clt, nil
}
return clt, nil
}

// LocalAgent is a getter function for the client's local agent
func (tc *TeleportClient) LocalAgent() *LocalKeyAgent {
return tc.localAgent
Expand Down
6 changes: 3 additions & 3 deletions lib/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ func (proxy *ProxyClient) GetDatabaseServers(ctx context.Context, namespace stri
// CurrentClusterAccessPoint returns cluster access point to the currently
// selected cluster and is used for discovery
// and could be cached based on the access policy
func (proxy *ProxyClient) CurrentClusterAccessPoint(ctx context.Context, quiet bool) (auth.AccessPoint, error) {
func (proxy *ProxyClient) CurrentClusterAccessPoint(ctx context.Context, quiet bool) (auth.ClientI, error) {
// get the current cluster:
cluster, err := proxy.currentCluster()
if err != nil {
Expand All @@ -633,15 +633,15 @@ func (proxy *ProxyClient) CurrentClusterAccessPoint(ctx context.Context, quiet b

// ClusterAccessPoint returns cluster access point used for discovery
// and could be cached based on the access policy
func (proxy *ProxyClient) ClusterAccessPoint(ctx context.Context, clusterName string, quiet bool) (auth.AccessPoint, error) {
func (proxy *ProxyClient) ClusterAccessPoint(ctx context.Context, clusterName string, quiet bool) (auth.ClientI, error) {
if clusterName == "" {
return nil, trace.BadParameter("parameter clusterName is missing")
}
clt, err := proxy.ConnectToCluster(ctx, clusterName, quiet)
if err != nil {
return nil, trace.Wrap(err)
}
return proxy.teleportClient.accessPoint(clt, proxy.proxyAddress, clusterName)
return clt, nil
}

// ConnectToCurrentCluster connects to the auth server of the currently selected
Expand Down
2 changes: 1 addition & 1 deletion lib/reversetunnel/localsite.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (s *localSite) GetTunnelsCount() int {
return len(s.remoteConns)
}

// CachingAccessPoint returns a auth.AccessPoint for this cluster.
// CachingAccessPoint returns an auth.RemoteProxyAccessPoint for this cluster.
func (s *localSite) CachingAccessPoint() (auth.RemoteProxyAccessPoint, error) {
return s.accessPoint, nil
}
Expand Down
8 changes: 4 additions & 4 deletions lib/reversetunnel/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type server struct {
clusterPeers map[string]*clusterPeers

// newAccessPoint returns new caching access point
newAccessPoint auth.NewCachingAccessPoint
newAccessPoint auth.NewRemoteProxyCachingAccessPoint

// cancel function will cancel the
cancel context.CancelFunc
Expand Down Expand Up @@ -148,7 +148,7 @@ type Config struct {
LocalAccessPoint auth.ProxyAccessPoint
// NewCachingAccessPoint returns new caching access points
// per remote cluster
NewCachingAccessPoint auth.NewCachingAccessPoint
NewCachingAccessPoint auth.NewRemoteProxyCachingAccessPoint
// DirectClusters is a list of clusters accessed directly
DirectClusters []DirectCluster
// Context is a signalling context
Expand Down Expand Up @@ -198,7 +198,7 @@ type Config struct {
// NewCachingAccessPointOldProxy is an access point that can be configured
// with the old access point policy until all clusters are migrated to 7.0.0
// and above.
NewCachingAccessPointOldProxy auth.NewCachingAccessPoint
NewCachingAccessPointOldProxy auth.NewRemoteProxyCachingAccessPoint

// LockWatcher is a lock watcher.
LockWatcher *services.LockWatcher
Expand Down Expand Up @@ -1040,7 +1040,7 @@ func newRemoteSite(srv *server, domainName string, sconn ssh.Conn) (*remoteSite,
// don't assume the newer organization of cluster configuration resources
// (RFD 28) because older proxy servers will reject that causing the cache
// to go into a re-sync loop.
var accessPointFunc auth.NewCachingAccessPoint
var accessPointFunc auth.NewRemoteProxyCachingAccessPoint
ok, err := isPreV8Cluster(closeContext, sconn)
if err != nil {
return nil, trace.Wrap(err)
Expand Down
Loading

0 comments on commit f71301e

Please sign in to comment.