Debian Codegraph @

client/encryption.go

raw
1 // A REST client to interact with LINSTOR's REST API
2 // Copyright (C) LINBIT HA-Solutions GmbH
3 // All Rights Reserved.
4 // Author: Roland Kammerer <[email protected]>
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License"); you may
7 // not use this file except in compliance with the License. You may obtain
8 // a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 // License for the specific language governing permissions and limitations
16 // under the License.
17
18 package client
19
20 import "context"
21
22 // custom code
23
24 // EncryptionProvider acts as an abstraction for an EncryptionService. It can be
25 // swapped out for another EncryptionService implementation, for example for
26 // testing.
27 type EncryptionProvider interface {
28 // Create creates an encryption with the given passphrase
29 Create(ctx context.Context, passphrase Passphrase) error
30 // Modify modifies an existing passphrase
31 Modify(ctx context.Context, passphrase Passphrase) error
32 // Enter is used to enter a password so that content can be decrypted
33 Enter(ctx context.Context, password string) error
34 }
35
36 // EncryptionService is the service that deals with encyrption related tasks.
37 type EncryptionService struct {
38 client *Client
39 }
40
41 // Passphrase represents a LINSTOR passphrase
42 type Passphrase struct {
43 NewPassphrase string `json:"new_passphrase,omitempty"`
44 OldPassphrase string `json:"old_passphrase,omitempty"`
45 }
46
47 // Create creates an encryption with the given passphrase
48 func (n *EncryptionService) Create(ctx context.Context, passphrase Passphrase) error {
49 _, err := n.client.doPOST(ctx, "/v1/encryption/passphrase", passphrase)
50 return err
51 }
52
53 // Modify modifies an existing passphrase
54 func (n *EncryptionService) Modify(ctx context.Context, passphrase Passphrase) error {
55 _, err := n.client.doPUT(ctx, "/v1/encryption/passphrase", passphrase)
56 return err
57 }
58
59 // Enter is used to enter a password so that content can be decrypted
60 func (n *EncryptionService) Enter(ctx context.Context, password string) error {
61 _, err := n.client.doPATCH(ctx, "/v1/encryption/passphrase", password)
62 return err
63 }