Skip to content

Commit 7343ba0

Browse files
committed
fix: seed OAuth IS_*_ENABLED flags individually instead of all-or-nothing
The configure_instance management command checked whether *any* of the four IS_*_ENABLED keys (Google, GitHub, GitLab, Gitea) existed before deciding whether to create them. Because IS_GITEA_ENABLED was already seeded by instance_config_variables in the first loop, the exists() check always returned True and the remaining three keys were silently skipped. This meant IS_GITHUB_ENABLED, IS_GOOGLE_ENABLED, and IS_GITLAB_ENABLED were never created in the database. Toggling these providers on in God Mode sent a PATCH to update a non-existent row, which returned 200 OK but did nothing — the toggle appeared to succeed but reverted on page reload. Replace the all-or-nothing check with per-key get_or_create so each flag is seeded independently of the others. Signed-off-by: Mikhail Malyshev <mike.malyshev@gmail.com>
1 parent 058f7a2 commit 7343ba0

1 file changed

Lines changed: 53 additions & 109 deletions

File tree

apps/api/plane/license/management/commands/configure_instance.py

Lines changed: 53 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -40,113 +40,57 @@ def handle(self, *args, **options):
4040
else:
4141
self.stdout.write(self.style.WARNING(f"{obj.key} configuration already exists"))
4242

43-
keys = ["IS_GOOGLE_ENABLED", "IS_GITHUB_ENABLED", "IS_GITLAB_ENABLED", "IS_GITEA_ENABLED"]
44-
if not InstanceConfiguration.objects.filter(key__in=keys).exists():
45-
for key in keys:
46-
if key == "IS_GOOGLE_ENABLED":
47-
GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET = get_configuration_value(
48-
[
49-
{
50-
"key": "GOOGLE_CLIENT_ID",
51-
"default": os.environ.get("GOOGLE_CLIENT_ID", ""),
52-
},
53-
{
54-
"key": "GOOGLE_CLIENT_SECRET",
55-
"default": os.environ.get("GOOGLE_CLIENT_SECRET", "0"),
56-
},
57-
]
58-
)
59-
if bool(GOOGLE_CLIENT_ID) and bool(GOOGLE_CLIENT_SECRET):
60-
value = "1"
61-
else:
62-
value = "0"
63-
InstanceConfiguration.objects.create(
64-
key=key,
65-
value=value,
66-
category="AUTHENTICATION",
67-
is_encrypted=False,
68-
)
69-
self.stdout.write(self.style.SUCCESS(f"{key} loaded with value from environment variable."))
70-
if key == "IS_GITHUB_ENABLED":
71-
GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET = get_configuration_value(
72-
[
73-
{
74-
"key": "GITHUB_CLIENT_ID",
75-
"default": os.environ.get("GITHUB_CLIENT_ID", ""),
76-
},
77-
{
78-
"key": "GITHUB_CLIENT_SECRET",
79-
"default": os.environ.get("GITHUB_CLIENT_SECRET", "0"),
80-
},
81-
]
82-
)
83-
if bool(GITHUB_CLIENT_ID) and bool(GITHUB_CLIENT_SECRET):
84-
value = "1"
85-
else:
86-
value = "0"
87-
InstanceConfiguration.objects.create(
88-
key="IS_GITHUB_ENABLED",
89-
value=value,
90-
category="AUTHENTICATION",
91-
is_encrypted=False,
92-
)
93-
self.stdout.write(self.style.SUCCESS(f"{key} loaded with value from environment variable."))
94-
if key == "IS_GITLAB_ENABLED":
95-
GITLAB_HOST, GITLAB_CLIENT_ID, GITLAB_CLIENT_SECRET = get_configuration_value(
96-
[
97-
{
98-
"key": "GITLAB_HOST",
99-
"default": os.environ.get("GITLAB_HOST", "https://gitlab.com"),
100-
},
101-
{
102-
"key": "GITLAB_CLIENT_ID",
103-
"default": os.environ.get("GITLAB_CLIENT_ID", ""),
104-
},
105-
{
106-
"key": "GITLAB_CLIENT_SECRET",
107-
"default": os.environ.get("GITLAB_CLIENT_SECRET", ""),
108-
},
109-
]
110-
)
111-
if bool(GITLAB_HOST) and bool(GITLAB_CLIENT_ID) and bool(GITLAB_CLIENT_SECRET):
112-
value = "1"
113-
else:
114-
value = "0"
115-
InstanceConfiguration.objects.create(
116-
key="IS_GITLAB_ENABLED",
117-
value=value,
118-
category="AUTHENTICATION",
119-
is_encrypted=False,
120-
)
121-
self.stdout.write(self.style.SUCCESS(f"{key} loaded with value from environment variable."))
122-
if key == "IS_GITEA_ENABLED":
123-
GITEA_HOST, GITEA_CLIENT_ID, GITEA_CLIENT_SECRET = get_configuration_value(
124-
[
125-
{
126-
"key": "GITEA_HOST",
127-
"default": os.environ.get("GITEA_HOST", ""),
128-
},
129-
{
130-
"key": "GITEA_CLIENT_ID",
131-
"default": os.environ.get("GITEA_CLIENT_ID", ""),
132-
},
133-
{
134-
"key": "GITEA_CLIENT_SECRET",
135-
"default": os.environ.get("GITEA_CLIENT_SECRET", ""),
136-
},
137-
]
138-
)
139-
if bool(GITEA_HOST) and bool(GITEA_CLIENT_ID) and bool(GITEA_CLIENT_SECRET):
140-
value = "1"
141-
else:
142-
value = "0"
143-
InstanceConfiguration.objects.create(
144-
key="IS_GITEA_ENABLED",
145-
value=value,
146-
category="AUTHENTICATION",
147-
is_encrypted=False,
148-
)
149-
self.stdout.write(self.style.SUCCESS(f"{key} loaded with value from environment variable."))
150-
else:
151-
for key in keys:
43+
# Seed IS_*_ENABLED flags individually so that each missing key
44+
# is created independently. The previous all-or-nothing check
45+
# skipped every key when even one already existed (e.g.
46+
# IS_GITEA_ENABLED seeded via instance_config_variables).
47+
oauth_enabled_keys = {
48+
"IS_GOOGLE_ENABLED": lambda: all(
49+
get_configuration_value(
50+
[
51+
{"key": "GOOGLE_CLIENT_ID", "default": os.environ.get("GOOGLE_CLIENT_ID", "")},
52+
{"key": "GOOGLE_CLIENT_SECRET", "default": os.environ.get("GOOGLE_CLIENT_SECRET", "")},
53+
]
54+
)
55+
),
56+
"IS_GITHUB_ENABLED": lambda: all(
57+
get_configuration_value(
58+
[
59+
{"key": "GITHUB_CLIENT_ID", "default": os.environ.get("GITHUB_CLIENT_ID", "")},
60+
{"key": "GITHUB_CLIENT_SECRET", "default": os.environ.get("GITHUB_CLIENT_SECRET", "")},
61+
]
62+
)
63+
),
64+
"IS_GITLAB_ENABLED": lambda: all(
65+
get_configuration_value(
66+
[
67+
{"key": "GITLAB_HOST", "default": os.environ.get("GITLAB_HOST", "")},
68+
{"key": "GITLAB_CLIENT_ID", "default": os.environ.get("GITLAB_CLIENT_ID", "")},
69+
{"key": "GITLAB_CLIENT_SECRET", "default": os.environ.get("GITLAB_CLIENT_SECRET", "")},
70+
]
71+
)
72+
),
73+
"IS_GITEA_ENABLED": lambda: all(
74+
get_configuration_value(
75+
[
76+
{"key": "GITEA_HOST", "default": os.environ.get("GITEA_HOST", "")},
77+
{"key": "GITEA_CLIENT_ID", "default": os.environ.get("GITEA_CLIENT_ID", "")},
78+
{"key": "GITEA_CLIENT_SECRET", "default": os.environ.get("GITEA_CLIENT_SECRET", "")},
79+
]
80+
)
81+
),
82+
}
83+
84+
for key, check_configured in oauth_enabled_keys.items():
85+
obj, created = InstanceConfiguration.objects.get_or_create(
86+
key=key,
87+
defaults={
88+
"value": "1" if check_configured() else "0",
89+
"category": "AUTHENTICATION",
90+
"is_encrypted": False,
91+
},
92+
)
93+
if created:
94+
self.stdout.write(self.style.SUCCESS(f"{key} loaded with value from environment variable."))
95+
else:
15296
self.stdout.write(self.style.WARNING(f"{key} configuration already exists"))

0 commit comments

Comments
 (0)