|
| 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 local |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "github.com/gravitational/trace" |
| 23 | + |
| 24 | + headerv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/header/v1" |
| 25 | + recordingencryptionv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/recordingencryption/v1" |
| 26 | + "github.com/gravitational/teleport/api/types" |
| 27 | + "github.com/gravitational/teleport/lib/backend" |
| 28 | + "github.com/gravitational/teleport/lib/services" |
| 29 | + "github.com/gravitational/teleport/lib/services/local/generic" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + recordingEncryptionPrefix = "recording_encryption" |
| 34 | + recordingEncryptionConfigPrefix = "config" |
| 35 | +) |
| 36 | + |
| 37 | +// RecordingEncryptionService exposes backend functionality for working with the |
| 38 | +// cluster's RecordingEncryption resource. |
| 39 | +type RecordingEncryptionService struct { |
| 40 | + encryption *generic.ServiceWrapper[*recordingencryptionv1.RecordingEncryption] |
| 41 | +} |
| 42 | + |
| 43 | +var _ services.RecordingEncryption = (*RecordingEncryptionService)(nil) |
| 44 | + |
| 45 | +// NewRecordingEncryptionService creates a new RecordingEncryptionService. |
| 46 | +func NewRecordingEncryptionService(b backend.Backend) (*RecordingEncryptionService, error) { |
| 47 | + const pageLimit = 100 |
| 48 | + encryption, err := generic.NewServiceWrapper(generic.ServiceConfig[*recordingencryptionv1.RecordingEncryption]{ |
| 49 | + Backend: b, |
| 50 | + PageLimit: pageLimit, |
| 51 | + ResourceKind: types.KindRecordingEncryption, |
| 52 | + BackendPrefix: backend.NewKey(recordingEncryptionPrefix), |
| 53 | + MarshalFunc: services.MarshalProtoResource[*recordingencryptionv1.RecordingEncryption], |
| 54 | + UnmarshalFunc: services.UnmarshalProtoResource[*recordingencryptionv1.RecordingEncryption], |
| 55 | + }) |
| 56 | + if err != nil { |
| 57 | + return nil, trace.Wrap(err) |
| 58 | + } |
| 59 | + |
| 60 | + return &RecordingEncryptionService{ |
| 61 | + encryption: encryption, |
| 62 | + }, nil |
| 63 | +} |
| 64 | + |
| 65 | +// CreateRecordingEncryption creates a new RecordingEncryption in the backend. |
| 66 | +func (s *RecordingEncryptionService) CreateRecordingEncryption(ctx context.Context, encryption *recordingencryptionv1.RecordingEncryption) (*recordingencryptionv1.RecordingEncryption, error) { |
| 67 | + if encryption.Metadata == nil { |
| 68 | + encryption.Metadata = &headerv1.Metadata{} |
| 69 | + } |
| 70 | + encryption.Metadata.Name = recordingEncryptionConfigPrefix |
| 71 | + created, err := s.encryption.CreateResource(ctx, encryption) |
| 72 | + return created, trace.Wrap(err) |
| 73 | +} |
| 74 | + |
| 75 | +// UpdateRecordingEncryption replaces the RecordingEncryption resource with the given one. |
| 76 | +func (s *RecordingEncryptionService) UpdateRecordingEncryption(ctx context.Context, encryption *recordingencryptionv1.RecordingEncryption) (*recordingencryptionv1.RecordingEncryption, error) { |
| 77 | + if encryption.Metadata == nil { |
| 78 | + encryption.Metadata = &headerv1.Metadata{} |
| 79 | + } |
| 80 | + encryption.Metadata.Name = recordingEncryptionConfigPrefix |
| 81 | + updated, err := s.encryption.ConditionalUpdateResource(ctx, encryption) |
| 82 | + return updated, trace.Wrap(err) |
| 83 | +} |
| 84 | + |
| 85 | +// DeleteRecordingEncryption removes the RecordingEncryption from the cluster. |
| 86 | +func (s *RecordingEncryptionService) DeleteRecordingEncryption(ctx context.Context) error { |
| 87 | + return trace.Wrap(s.encryption.DeleteResource(ctx, recordingEncryptionConfigPrefix)) |
| 88 | +} |
| 89 | + |
| 90 | +// GetRecordingEncryption retrieves the RecordingEncryption for the cluster. |
| 91 | +func (s *RecordingEncryptionService) GetRecordingEncryption(ctx context.Context) (*recordingencryptionv1.RecordingEncryption, error) { |
| 92 | + encryption, err := s.encryption.GetResource(ctx, recordingEncryptionConfigPrefix) |
| 93 | + return encryption, trace.Wrap(err) |
| 94 | +} |
| 95 | + |
| 96 | +type recordingEncryptionParser struct { |
| 97 | + baseParser |
| 98 | +} |
| 99 | + |
| 100 | +func newRecordingEncryptionParser() *recordingEncryptionParser { |
| 101 | + return &recordingEncryptionParser{ |
| 102 | + baseParser: newBaseParser(backend.NewKey(recordingEncryptionPrefix, recordingEncryptionConfigPrefix)), |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func (p *recordingEncryptionParser) parse(event backend.Event) (types.Resource, error) { |
| 107 | + switch event.Type { |
| 108 | + case types.OpPut: |
| 109 | + resource, err := services.UnmarshalProtoResource[*recordingencryptionv1.RecordingEncryption]( |
| 110 | + event.Item.Value, |
| 111 | + services.WithExpires(event.Item.Expires), |
| 112 | + services.WithRevision(event.Item.Revision), |
| 113 | + ) |
| 114 | + if err != nil { |
| 115 | + return nil, trace.Wrap(err, "unmarshalling resource from event") |
| 116 | + } |
| 117 | + return types.Resource153ToLegacy(resource), nil |
| 118 | + case types.OpDelete: |
| 119 | + resource, err := services.UnmarshalProtoResource[*recordingencryptionv1.RecordingEncryption]( |
| 120 | + event.Item.Value, |
| 121 | + services.WithExpires(event.Item.Expires), |
| 122 | + services.WithRevision(event.Item.Revision), |
| 123 | + ) |
| 124 | + if err != nil { |
| 125 | + return nil, trace.Wrap(err, "unmarshalling resource from event") |
| 126 | + } |
| 127 | + return types.Resource153ToLegacy(resource), nil |
| 128 | + default: |
| 129 | + return nil, trace.BadParameter("event %v is not supported", event.Type) |
| 130 | + } |
| 131 | +} |
0 commit comments