Coverage for src/couchers/config.py: 52%

46 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-07-22 17:19 +0000

1""" 

2A simple config system 

3""" 

4 

5import os 

6 

7# Allowed config options, as tuples (name, type, default). 

8# All fields are required 

9CONFIG_OPTIONS = [ 

10 # Whether we're in dev mode 

11 ("DEV", bool), 

12 # Whether we're `api` mode (answering API queries) or `scheduler` (scheduling background jobs), or `worker` 

13 # (servicing background jobs). Can also be set to `all` to do all three simultaneously 

14 ("ROLE", ["api", "scheduler", "worker", "all"], "all"), 

15 # Version string 

16 ("VERSION", str, "unknown"), 

17 # Base URL of frontend, e.g. https://couchers.org 

18 ("BASE_URL", str), 

19 # URL of the backend, e.g. https://api.couchers.org 

20 ("BACKEND_BASE_URL", str), 

21 # URL of the console, e.g. https://console.couchers.org 

22 ("CONSOLE_BASE_URL", str), 

23 # Used to generate a variety of secrets 

24 ("SECRET", bytes), 

25 # Domain that cookies should set as their domain value 

26 ("COOKIE_DOMAIN", str), 

27 # SQLAlchemy database connection string 

28 ("DATABASE_CONNECTION_STRING", str), 

29 # Whether to try adding dummy data 

30 ("ADD_DUMMY_DATA", bool), 

31 # Donations 

32 ("ENABLE_DONATIONS", bool), 

33 ("STRIPE_API_KEY", str), 

34 ("STRIPE_WEBHOOK_SECRET", str), 

35 ("STRIPE_RECURRING_PRODUCT_ID", str), 

36 # Strong verification through Iris ID 

37 ("ENABLE_STRONG_VERIFICATION", bool), 

38 ("IRIS_ID_PUBKEY", str), 

39 ("IRIS_ID_SECRET", str), 

40 ("VERIFICATION_DATA_PUBLIC_KEY", bytes), 

41 # SMS 

42 ("ENABLE_SMS", bool), 

43 ("SMS_SENDER_ID", str), 

44 # Email 

45 ("ENABLE_EMAIL", bool), 

46 # Sender name for outgoing notification emails e.g. "Couchers.org" 

47 ("NOTIFICATION_EMAIL_SENDER", str), 

48 # Sender email, e.g. "notify@couchers.org" 

49 ("NOTIFICATION_EMAIL_ADDRESS", str), 

50 # An optional prefix for email subject, e.g. [STAGING] 

51 ("NOTIFICATION_EMAIL_PREFIX", str, ""), 

52 # Address to send emails about reported users 

53 ("REPORTS_EMAIL_RECIPIENT", str), 

54 # Address to send contributor forms when users sign up/fill the form 

55 ("CONTRIBUTOR_FORM_EMAIL_RECIPIENT", str), 

56 # Address to moderation notifications 

57 ("MODS_EMAIL_RECIPIENT", str), 

58 # SMTP settings 

59 ("SMTP_HOST", str), 

60 ("SMTP_PORT", int), 

61 ("SMTP_USERNAME", str), 

62 ("SMTP_PASSWORD", str), 

63 # Media server 

64 ("ENABLE_MEDIA", bool), 

65 ("MEDIA_SERVER_SECRET_KEY", bytes), 

66 ("MEDIA_SERVER_BEARER_TOKEN", str), 

67 ("MEDIA_SERVER_BASE_URL", str), 

68 # Bug reporting tool 

69 ("BUG_TOOL_ENABLED", bool), 

70 ("BUG_TOOL_GITHUB_REPO", str), 

71 ("BUG_TOOL_GITHUB_USERNAME", str), 

72 ("BUG_TOOL_GITHUB_TOKEN", str), 

73 # Sentry 

74 ("SENTRY_ENABLED", bool), 

75 ("SENTRY_URL", str), 

76 # Push notifications 

77 ("PUSH_NOTIFICATIONS_ENABLED", bool), 

78 ("PUSH_NOTIFICATIONS_VAPID_PRIVATE_KEY", str), 

79 ("PUSH_NOTIFICATIONS_VAPID_SUBJECT", str), 

80 # Listmonk (mailing list) 

81 ("LISTMONK_ENABLED", bool), 

82 ("LISTMONK_BASE_URL", str), 

83 ("LISTMONK_API_KEY", str), 

84 ("LISTMONK_LIST_UUID", str), 

85 # Whether we're in test 

86 ("IN_TEST", bool, "0"), 

87] 

88 

89config = {} 

90 

91for config_option in CONFIG_OPTIONS: 

92 if len(config_option) == 2: 

93 name, type_ = config_option 

94 optional = False 

95 elif len(config_option) == 3: 

96 name, type_, default_value = config_option 

97 optional = True 

98 else: 

99 raise ValueError("Invalid CONFIG_OPTIONS") 

100 

101 value = os.getenv(name) 

102 

103 if not value: 

104 if not optional: 

105 # config value not set - will cause a KeyError when trying 

106 # to access it. 

107 continue 

108 else: 

109 value = default_value 

110 

111 if type_ is bool: 

112 # 1 is true, 0 is false, everything else is illegal 

113 if value not in ["0", "1"]: 

114 raise ValueError(f'Invalid bool for {name}, need "0" or "1"') 

115 value = value == "1" 

116 elif type_ is bytes: 

117 # decode from hex 

118 value = bytes.fromhex(value) 

119 elif isinstance(type_, list): 

120 # list of allowed string values 

121 if value not in type_: 

122 raise ValueError(f'Invalid value for {name}, need one of {", ".join(type_)}') 

123 else: 

124 value = type_(value) 

125 

126 config[name] = value 

127 

128 

129## Config checks 

130def check_config(): 

131 for name, *_ in CONFIG_OPTIONS: 

132 if name not in config: 

133 raise ValueError(f"Required config value {name} not set") 

134 

135 if not config["DEV"]: 

136 # checks for prod 

137 if "https" not in config["BASE_URL"]: 

138 raise Exception("Production site must be over HTTPS") 

139 if not config["ENABLE_EMAIL"]: 

140 raise Exception("Production site must have email enabled") 

141 if not config["ENABLE_SMS"]: 

142 raise Exception("Production site must have SMS enabled") 

143 if config["IN_TEST"]: 

144 raise Exception("IN_TEST while not DEV") 

145 

146 if config["ENABLE_DONATIONS"]: 

147 if ( 

148 not config["STRIPE_API_KEY"] 

149 or not config["STRIPE_WEBHOOK_SECRET"] 

150 or not config["STRIPE_RECURRING_PRODUCT_ID"] 

151 ): 

152 raise Exception("No Stripe API key/recurring donation ID but donations enabled") 

153 

154 if config["ENABLE_STRONG_VERIFICATION"]: 

155 if not config["IRIS_ID_PUBKEY"] or not config["IRIS_ID_SECRET"] or not config["VERIFICATION_DATA_PUBLIC_KEY"]: 

156 raise Exception("No Iris ID pubkey/secret or verification data pubkey but strong verification enabled")