Coverage for src/edwh_restic_plugin/repositories/s3.py: 42%

1 import os

2

3 from edwh.helpers import generate_password

4 from invoke import Context

5

6 from . import Repository, register

7

8

9@register()

10 class S3Repository(Repository):

11 # todo: currently tested on Oracle s3 compat and other non-Amazon S3 compatible services, so check with actual S3!

12

13 def setup(self) -> None:

14 self.check_env(

15 "S3_NAME",

16 default=None,

17 comment="Bucket to store your backups",

18 )

19

20 self.check_env(

21 "S3_PASSWORD",

22 default=generate_password(silent=True),

23 comment="Create a Restic password, keep it safe.",

24 )

25

26 self.check_env("S3_URL", default=None, comment="Full S3 storage URL")

27

28 self.check_env(

29 "S3_ACCESS_KEY_ID", default=None, comment="Specifies an AWS access key associated with an IAM account."

30 )

31 self.check_env(

32 "S3_SECRET_ACCESS_KEY", default=None, comment="Specifies the secret key associated with the access key."

33 )

34

35 def prepare_for_restic(self, _: Context) -> None:

36 env = self.env_config

37 os.environ["RESTIC_PASSWORD"] = env["S3_PASSWORD"]

38 os.environ["RESTIC_REPOSITORY"] = self.uri

39

40 os.environ["AWS_ACCESS_KEY_ID"] = env["S3_ACCESS_KEY_ID"]

41 os.environ["AWS_SECRET_ACCESS_KEY"] = env["S3_SECRET_ACCESS_KEY"]

42

43 @property

44 def uri(self) -> str:

45 bucket = self.env_config["S3_NAME"]

46 base = self.env_config["S3_URL"]

47 # make sure prefix and suffix are there but only once:

48 base = base.removeprefix("s3:").removesuffix(f"/{bucket}").strip("/")

49 return f"s3:{base}/{bucket}"