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

46 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-04-16 15:13 +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 # number of bg worker processes, requires worker or all above 

16 ("BACKGROUND_WORKER_COUNT", int, 2), 

17 # Version string 

18 ("VERSION", str, "unknown"), 

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

20 ("BASE_URL", str), 

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

22 ("BACKEND_BASE_URL", str), 

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

24 ("CONSOLE_BASE_URL", str), 

25 # Used to generate a variety of secrets 

26 ("SECRET", bytes), 

27 # Domain that cookies should set as their domain value 

28 ("COOKIE_DOMAIN", str), 

29 # SQLAlchemy database connection string 

30 ("DATABASE_CONNECTION_STRING", str), 

31 # OpenTelemetry endpoint to send traces to 

32 ("OPENTELEMETRY_ENDPOINT", str, ""), 

33 # Path to a GeoLite2-City.mmdb file for geocoding IPs in user session info 

34 ("GEOLITE2_CITY_MMDB_FILE_LOCATION", str, ""), 

35 # Whether to try adding dummy data 

36 ("ADD_DUMMY_DATA", bool), 

37 # Donations 

38 ("ENABLE_DONATIONS", bool), 

39 ("STRIPE_API_KEY", str), 

40 ("STRIPE_WEBHOOK_SECRET", str), 

41 ("STRIPE_RECURRING_PRODUCT_ID", str), 

42 # Strong verification through Iris ID 

43 ("ENABLE_STRONG_VERIFICATION", bool), 

44 ("IRIS_ID_PUBKEY", str), 

45 ("IRIS_ID_SECRET", str), 

46 ("VERIFICATION_DATA_PUBLIC_KEY", bytes), 

47 # SMS 

48 ("ENABLE_SMS", bool), 

49 ("SMS_SENDER_ID", str), 

50 # Email 

51 ("ENABLE_EMAIL", bool), 

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

53 ("NOTIFICATION_EMAIL_SENDER", str), 

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

55 ("NOTIFICATION_EMAIL_ADDRESS", str), 

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

57 ("NOTIFICATION_PREFIX", str, ""), 

58 # Address to send emails about reported users 

59 ("REPORTS_EMAIL_RECIPIENT", str), 

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

61 ("CONTRIBUTOR_FORM_EMAIL_RECIPIENT", str), 

62 # Address to moderation notifications 

63 ("MODS_EMAIL_RECIPIENT", str), 

64 # SMTP settings 

65 ("SMTP_HOST", str), 

66 ("SMTP_PORT", int), 

67 ("SMTP_USERNAME", str), 

68 ("SMTP_PASSWORD", str), 

69 # Media server 

70 ("ENABLE_MEDIA", bool), 

71 ("MEDIA_SERVER_SECRET_KEY", bytes), 

72 ("MEDIA_SERVER_BEARER_TOKEN", str), 

73 ("MEDIA_SERVER_BASE_URL", str), 

74 ("MEDIA_SERVER_UPLOAD_BASE_URL", str), 

75 # Bug reporting tool 

76 ("BUG_TOOL_ENABLED", bool), 

77 ("BUG_TOOL_GITHUB_REPO", str), 

78 ("BUG_TOOL_GITHUB_USERNAME", str), 

79 ("BUG_TOOL_GITHUB_TOKEN", str), 

80 # Sentry 

81 ("SENTRY_ENABLED", bool), 

82 ("SENTRY_URL", str), 

83 # Push notifications 

84 ("PUSH_NOTIFICATIONS_ENABLED", bool), 

85 ("PUSH_NOTIFICATIONS_VAPID_PRIVATE_KEY", str), 

86 ("PUSH_NOTIFICATIONS_VAPID_SUBJECT", str), 

87 # Whether to initiate new activeness probes 

88 ("ACTIVENESS_PROBES_ENABLED", bool), 

89 # Listmonk (mailing list) 

90 ("LISTMONK_ENABLED", bool), 

91 ("LISTMONK_BASE_URL", str), 

92 ("LISTMONK_API_USERNAME", str), 

93 ("LISTMONK_API_KEY", str), 

94 ("LISTMONK_LIST_ID", int), 

95 # Whether we're in test 

96 ("IN_TEST", bool, "0"), 

97] 

98 

99config = {} 

100 

101for config_option in CONFIG_OPTIONS: 

102 if len(config_option) == 2: 

103 name, type_ = config_option 

104 optional = False 

105 elif len(config_option) == 3: 

106 name, type_, default_value = config_option 

107 optional = True 

108 else: 

109 raise ValueError("Invalid CONFIG_OPTIONS") 

110 

111 value = os.getenv(name) 

112 

113 if not value: 

114 if not optional: 

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

116 # to access it. 

117 continue 

118 else: 

119 value = default_value 

120 

121 if type_ is bool: 

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

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

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

125 value = value == "1" 

126 elif type_ is bytes: 

127 # decode from hex 

128 value = bytes.fromhex(value) 

129 elif isinstance(type_, list): 

130 # list of allowed string values 

131 if value not in type_: 

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

133 else: 

134 value = type_(value) 

135 

136 config[name] = value 

137 

138 

139## Config checks 

140def check_config(): 

141 for name, *_ in CONFIG_OPTIONS: 

142 if name not in config: 

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

144 

145 if not config["DEV"]: 

146 # checks for prod 

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

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

149 if not config["ENABLE_EMAIL"]: 

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

151 if not config["ENABLE_SMS"]: 

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

153 if config["IN_TEST"]: 

154 raise Exception("IN_TEST while not DEV") 

155 

156 if config["ENABLE_DONATIONS"]: 

157 if ( 

158 not config["STRIPE_API_KEY"] 

159 or not config["STRIPE_WEBHOOK_SECRET"] 

160 or not config["STRIPE_RECURRING_PRODUCT_ID"] 

161 ): 

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

163 

164 if config["ENABLE_STRONG_VERIFICATION"]: 

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

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