|
| 1 | +// Teleport |
| 2 | +// Copyright (C) 2025 Gravitational, Inc. |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package cache |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "github.com/gravitational/trace" |
| 23 | + |
| 24 | + "github.com/gravitational/teleport/api/types" |
| 25 | + "github.com/gravitational/teleport/lib/services" |
| 26 | +) |
| 27 | + |
| 28 | +type networkingRestrictionIndex string |
| 29 | + |
| 30 | +const networkingRestrictionNameIndex networkingRestrictionIndex = "name" |
| 31 | + |
| 32 | +func newNetworkingRestrictionCollection(upstream services.Restrictions, w types.WatchKind) (*collection[types.NetworkRestrictions, networkingRestrictionIndex], error) { |
| 33 | + if upstream == nil { |
| 34 | + return nil, trace.BadParameter("missing parameter Restrictions") |
| 35 | + } |
| 36 | + |
| 37 | + return &collection[types.NetworkRestrictions, networkingRestrictionIndex]{ |
| 38 | + store: newStore(map[networkingRestrictionIndex]func(types.NetworkRestrictions) string{ |
| 39 | + networkingRestrictionNameIndex: types.NetworkRestrictions.GetName, |
| 40 | + }), |
| 41 | + fetcher: func(ctx context.Context, loadSecrets bool) ([]types.NetworkRestrictions, error) { |
| 42 | + restrictions, err := upstream.GetNetworkRestrictions(ctx) |
| 43 | + if err != nil { |
| 44 | + return nil, trace.Wrap(err) |
| 45 | + } |
| 46 | + return []types.NetworkRestrictions{restrictions}, nil |
| 47 | + }, |
| 48 | + headerTransform: func(hdr *types.ResourceHeader) types.NetworkRestrictions { |
| 49 | + return &types.NetworkRestrictionsV4{ |
| 50 | + Kind: hdr.Kind, |
| 51 | + Version: hdr.Version, |
| 52 | + Metadata: types.Metadata{ |
| 53 | + Name: hdr.Metadata.Name, |
| 54 | + }, |
| 55 | + } |
| 56 | + }, |
| 57 | + watch: w, |
| 58 | + }, nil |
| 59 | +} |
| 60 | + |
| 61 | +// GetNetworkRestrictions gets the network restrictions. |
| 62 | +func (c *Cache) GetNetworkRestrictions(ctx context.Context) (types.NetworkRestrictions, error) { |
| 63 | + ctx, span := c.Tracer.Start(ctx, "cache/GetNetworkRestrictions") |
| 64 | + defer span.End() |
| 65 | + |
| 66 | + getter := genericGetter[types.NetworkRestrictions, networkingRestrictionIndex]{ |
| 67 | + cache: c, |
| 68 | + collection: c.collections.networkRestrictions, |
| 69 | + index: networkingRestrictionNameIndex, |
| 70 | + upstreamGet: func(ctx context.Context, s string) (types.NetworkRestrictions, error) { |
| 71 | + restriction, err := c.Config.Restrictions.GetNetworkRestrictions(ctx) |
| 72 | + return restriction, trace.Wrap(err) |
| 73 | + }, |
| 74 | + clone: types.NetworkRestrictions.Clone, |
| 75 | + } |
| 76 | + out, err := getter.get(ctx, types.MetaNameNetworkRestrictions) |
| 77 | + return out, trace.Wrap(err) |
| 78 | +} |
0 commit comments