|
| 1 | +package storage |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + |
| 7 | + "gorm.io/gorm" |
| 8 | + |
| 9 | + "get.porter.sh/porter/pkg/secrets" |
| 10 | + hostSecrets "get.porter.sh/porter/pkg/secrets/plugins/host" |
| 11 | + "get.porter.sh/porter/pkg/tracing" |
| 12 | +) |
| 13 | + |
| 14 | +var _ CredentialSetProvider = (*CredentialStoreSQL)(nil) |
| 15 | + |
| 16 | +// CredentialStoreSQL is a wrapper around Porter's datastore |
| 17 | +// providing typed access and additional business logic around |
| 18 | +// credential sets, usually referred to as "credentials" as a shorthand. |
| 19 | +type CredentialStoreSQL struct { |
| 20 | + db *gorm.DB |
| 21 | + Secrets secrets.Store |
| 22 | + HostSecrets hostSecrets.Store |
| 23 | +} |
| 24 | + |
| 25 | +func NewCredentialStoreSQL(db *gorm.DB, secrets secrets.Store) *CredentialStoreSQL { |
| 26 | + return &CredentialStoreSQL{ |
| 27 | + db: db, |
| 28 | + Secrets: secrets, |
| 29 | + HostSecrets: hostSecrets.NewStore(), |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/* |
| 34 | + Secrets |
| 35 | +*/ |
| 36 | + |
| 37 | +func (s CredentialStoreSQL) ResolveAll(ctx context.Context, creds CredentialSet) (secrets.Set, error) { |
| 38 | + return resolveAll(ctx, creds.Credentials, s.HostSecrets, s.Secrets, creds.Name, "credential") |
| 39 | +} |
| 40 | + |
| 41 | +func (s CredentialStoreSQL) Validate(ctx context.Context, creds CredentialSet) error { |
| 42 | + return validate(creds.Credentials) |
| 43 | +} |
| 44 | + |
| 45 | +/* |
| 46 | + Document Storage |
| 47 | +*/ |
| 48 | + |
| 49 | +func (s CredentialStoreSQL) InsertCredentialSet(ctx context.Context, creds CredentialSet) error { |
| 50 | + creds.SchemaVersion = DefaultCredentialSetSchemaVersion |
| 51 | + return s.db.WithContext(ctx).Create(&creds).Error |
| 52 | +} |
| 53 | + |
| 54 | +func (s CredentialStoreSQL) ListCredentialSets(ctx context.Context, listOptions ListOptions) ([]CredentialSet, error) { |
| 55 | + _, log := tracing.StartSpan(ctx) |
| 56 | + defer log.EndSpan() |
| 57 | + |
| 58 | + var out []CredentialSet |
| 59 | + |
| 60 | + // If no filters are provided, return an empty list |
| 61 | + if listOptions.Namespace == "" && listOptions.Name == "" && len(listOptions.Labels) == 0 { |
| 62 | + return out, nil |
| 63 | + } |
| 64 | + |
| 65 | + query := s.db.WithContext(ctx).Order("namespace ASC, name ASC") |
| 66 | + |
| 67 | + if listOptions.Namespace != "" && listOptions.Namespace != "*" { |
| 68 | + query = query.Where("namespace = ?", listOptions.Namespace) |
| 69 | + } |
| 70 | + |
| 71 | + if listOptions.Name != "" { |
| 72 | + query = query.Where("name LIKE ?", "%"+listOptions.Name+"%") |
| 73 | + } |
| 74 | + |
| 75 | + // Filter by Labels |
| 76 | + if len(listOptions.Labels) > 0 { |
| 77 | + for key, value := range listOptions.Labels { |
| 78 | + query = query.Where("labels->? = ?", key, value) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if listOptions.Skip > 0 { |
| 83 | + query = query.Offset(int(listOptions.Skip)) |
| 84 | + } |
| 85 | + |
| 86 | + if listOptions.Limit > 0 { |
| 87 | + query = query.Limit(int(listOptions.Limit)) |
| 88 | + } |
| 89 | + |
| 90 | + err := query.Find(&out).Error |
| 91 | + if err != nil { |
| 92 | + return nil, log.Error(err) |
| 93 | + } |
| 94 | + return out, err |
| 95 | +} |
| 96 | + |
| 97 | +func (s CredentialStoreSQL) GetCredentialSet(ctx context.Context, namespace string, name string) (CredentialSet, error) { |
| 98 | + var out CredentialSet |
| 99 | + err := s.db.WithContext(ctx). |
| 100 | + Where("namespace = ? AND name = ?", namespace, name). |
| 101 | + First(&out).Error |
| 102 | + if errors.Is(err, gorm.ErrRecordNotFound) { |
| 103 | + return CredentialSet{}, ErrNotFound{Collection: "credentials"} |
| 104 | + } |
| 105 | + return out, err |
| 106 | +} |
| 107 | +func (s CredentialStoreSQL) FindCredentialSet(ctx context.Context, namespace string, name string) (CredentialSet, error) { |
| 108 | + var out CredentialSet |
| 109 | + |
| 110 | + query := s.db.WithContext(ctx). |
| 111 | + Where("name = ?", name). |
| 112 | + Where("(namespace = ? OR namespace = '' OR namespace IS NULL)", namespace). |
| 113 | + Order("namespace DESC") // DESC ensures that the namespace is prioritized over the empty or null |
| 114 | + |
| 115 | + err := query.First(&out).Error |
| 116 | + if errors.Is(err, gorm.ErrRecordNotFound) { |
| 117 | + return CredentialSet{}, ErrNotFound{Collection: "credentials"} |
| 118 | + } |
| 119 | + return out, err |
| 120 | +} |
| 121 | + |
| 122 | +func (s CredentialStoreSQL) UpdateCredentialSet(ctx context.Context, creds CredentialSet) error { |
| 123 | + creds.SchemaVersion = DefaultCredentialSetSchemaVersion |
| 124 | + return s.db.WithContext(ctx). |
| 125 | + Where("namespace = ? AND name = ?", creds.Namespace, creds.Name). |
| 126 | + Save(&creds).Error |
| 127 | +} |
| 128 | + |
| 129 | +func (s CredentialStoreSQL) UpsertCredentialSet(ctx context.Context, creds CredentialSet) error { |
| 130 | + creds.SchemaVersion = DefaultCredentialSetSchemaVersion |
| 131 | + return s.db.WithContext(ctx).Save(&creds).Error |
| 132 | +} |
| 133 | +func (s CredentialStoreSQL) RemoveCredentialSet(ctx context.Context, namespace string, name string) error { |
| 134 | + return s.db.WithContext(ctx).Where("namespace = ? AND name = ?", namespace, name).Delete(&CredentialSet{}).Error |
| 135 | +} |
0 commit comments