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

46 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-06-01 15:07 +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 ("GEOLITE2_ASN_MMDB_FILE_LOCATION", str, ""), 

36 # Whether to try adding dummy data 

37 ("ADD_DUMMY_DATA", bool), 

38 # Donations 

39 ("ENABLE_DONATIONS", bool), 

40 ("STRIPE_API_KEY", str), 

41 ("STRIPE_WEBHOOK_SECRET", str), 

42 ("STRIPE_RECURRING_PRODUCT_ID", str), 

43 # Strong verification through Iris ID 

44 ("ENABLE_STRONG_VERIFICATION", bool), 

45 ("IRIS_ID_PUBKEY", str), 

46 ("IRIS_ID_SECRET", str), 

47 ("VERIFICATION_DATA_PUBLIC_KEY", bytes), 

48 # SMS 

49 ("ENABLE_SMS", bool), 

50 ("SMS_SENDER_ID", str), 

51 # Email 

52 ("ENABLE_EMAIL", bool), 

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

54 ("NOTIFICATION_EMAIL_SENDER", str), 

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

56 ("NOTIFICATION_EMAIL_ADDRESS", str), 

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

58 ("NOTIFICATION_PREFIX", str, ""), 

59 # Address to send emails about reported users 

60 ("REPORTS_EMAIL_RECIPIENT", str), 

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

62 ("CONTRIBUTOR_FORM_EMAIL_RECIPIENT", str), 

63 # Address to moderation notifications 

64 ("MODS_EMAIL_RECIPIENT", str), 

65 # SMTP settings 

66 ("SMTP_HOST", str), 

67 ("SMTP_PORT", int), 

68 ("SMTP_USERNAME", str), 

69 ("SMTP_PASSWORD", str), 

70 # Media server 

71 ("ENABLE_MEDIA", bool), 

72 ("MEDIA_SERVER_SECRET_KEY", bytes), 

73 ("MEDIA_SERVER_BEARER_TOKEN", str), 

74 ("MEDIA_SERVER_BASE_URL", str), 

75 ("MEDIA_SERVER_UPLOAD_BASE_URL", str), 

76 # Bug reporting tool 

77 ("BUG_TOOL_ENABLED", bool), 

78 ("BUG_TOOL_GITHUB_REPO", str), 

79 ("BUG_TOOL_GITHUB_USERNAME", str), 

80 ("BUG_TOOL_GITHUB_TOKEN", str), 

81 # Sentry 

82 ("SENTRY_ENABLED", bool), 

83 ("SENTRY_URL", str), 

84 # Push notifications 

85 ("PUSH_NOTIFICATIONS_ENABLED", bool), 

86 ("PUSH_NOTIFICATIONS_VAPID_PRIVATE_KEY", str), 

87 ("PUSH_NOTIFICATIONS_VAPID_SUBJECT", str), 

88 # Whether to initiate new activeness probes 

89 ("ACTIVENESS_PROBES_ENABLED", bool), 

90 # Listmonk (mailing list) 

91 ("LISTMONK_ENABLED", bool), 

92 ("LISTMONK_BASE_URL", str), 

93 ("LISTMONK_API_USERNAME", str), 

94 ("LISTMONK_API_KEY", str), 

95 ("LISTMONK_LIST_ID", int), 

96 # Whether we're in test 

97 ("IN_TEST", bool, "0"), 

98] 

99 

100config = {} 

101 

102for config_option in CONFIG_OPTIONS: 

103 if len(config_option) == 2: 

104 name, type_ = config_option 

105 optional = False 

106 elif len(config_option) == 3: 

107 name, type_, default_value = config_option 

108 optional = True 

109 else: 

110 raise ValueError("Invalid CONFIG_OPTIONS") 

111 

112 value = os.getenv(name) 

113 

114 if not value: 

115 if not optional: 

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

117 # to access it. 

118 continue 

119 else: 

120 value = default_value 

121 

122 if type_ is bool: 

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

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

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

126 value = value == "1" 

127 elif type_ is bytes: 

128 # decode from hex 

129 value = bytes.fromhex(value) 

130 elif isinstance(type_, list): 

131 # list of allowed string values 

132 if value not in type_: 

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

134 else: 

135 value = type_(value) 

136 

137 config[name] = value 

138 

139 

140## Config checks 

141def check_config(): 

142 for name, *_ in CONFIG_OPTIONS: 

143 if name not in config: 

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

145 

146 if not config["DEV"]: 

147 # checks for prod 

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

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

150 if not config["ENABLE_EMAIL"]: 

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

152 if not config["ENABLE_SMS"]: 

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

154 if config["IN_TEST"]: 

155 raise Exception("IN_TEST while not DEV") 

156 

157 if config["ENABLE_DONATIONS"]: 

158 if ( 

159 not config["STRIPE_API_KEY"] 

160 or not config["STRIPE_WEBHOOK_SECRET"] 

161 or not config["STRIPE_RECURRING_PRODUCT_ID"] 

162 ): 

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

164 

165 if config["ENABLE_STRONG_VERIFICATION"]: 

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

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