Skip to content

Commit 5813d26

Browse files
committed
fix(guard): service account syntehtic email to match deployment base domain
1 parent 0a8bc7a commit 5813d26

File tree

7 files changed

+29
-18
lines changed

7 files changed

+29
-18
lines changed

guard/lib/guard/front_repo/user.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ defmodule Guard.FrontRepo.User do
251251
This validates the specific requirements for service account users.
252252
"""
253253
def service_account_changeset(user, params) do
254+
base_domain = Application.fetch_env!(:guard, :base_domain)
255+
escaped_domain = Regex.escape(base_domain)
256+
254257
user
255258
|> cast(params, [
256259
:email,
@@ -267,9 +270,9 @@ defmodule Guard.FrontRepo.User do
267270
|> validate_length(:name, max: 255, message: "Name cannot exceed 255 characters")
268271
|> validate_inclusion(:creation_source, [:service_account])
269272
|> put_change(:single_org_user, true)
270-
|> validate_format(:email, ~r/^[\w\-\.]+@sa\.[\w\-\.]+\.semaphoreci\.com$/i,
273+
|> validate_format(:email, ~r/^[\w\-\.]+@service_accounts\.[\w\-\.]+\.#{escaped_domain}$/i,
271274
message:
272-
"Service account email must follow the format: name@sa.organization.semaphoreci.com"
275+
"Service account email must follow the format: name@service_accounts.organization.#{base_domain}"
273276
)
274277
|> unique_constraint(:email, name: :index_users_on_email)
275278
|> unique_constraint(:authentication_token, name: :index_users_on_authentication_token)
@@ -283,8 +286,9 @@ defmodule Guard.FrontRepo.User do
283286
# Sanitize names to ensure valid email format
284287
sanitized_sa_name = sanitize_email_part(service_account_name)
285288
sanitized_org_name = sanitize_email_part(organization_name)
289+
base_domain = Application.fetch_env!(:guard, :base_domain)
286290

287-
"#{sanitized_sa_name}@sa.#{sanitized_org_name}.semaphoreci.com"
291+
"#{sanitized_sa_name}@service_accounts.#{sanitized_org_name}.#{base_domain}"
288292
end
289293

290294
defp sanitize_email_part(name) do

guard/lib/guard/store/service_account.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,14 +516,14 @@ defmodule Guard.Store.ServiceAccount do
516516
String.downcase(service_account_name) |> String.replace(~r/[^a-z0-9\-]/, "-")
517517

518518
sanitized_org = String.downcase(org_username) |> String.replace(~r/[^a-z0-9\-]/, "-")
519-
"#{sanitized_name}@sa.#{sanitized_org}.#{base_domain}"
519+
"#{sanitized_name}@service_accounts.#{sanitized_org}.#{base_domain}"
520520

521521
_ ->
522522
# Fallback if org not found (shouldn't happen in normal flow)
523523
sanitized_name =
524524
String.downcase(service_account_name) |> String.replace(~r/[^a-z0-9\-]/, "-")
525525

526-
"#{sanitized_name}@sa.unknown.#{base_domain}"
526+
"#{sanitized_name}@service_accounts.unknown.#{base_domain}"
527527
end
528528
end
529529

guard/test/guard/grpc_servers/user_server_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ defmodule Guard.GrpcServers.UserServerTest do
13861386
assert user_id == user.id
13871387
assert user_email == user.email
13881388
assert user_name == user.name
1389-
assert String.contains?(user_email, "@sa.")
1389+
assert String.contains?(user_email, "@service_accounts.")
13901390
assert String.contains?(user_email, ".#{Application.fetch_env!(:guard, :base_domain)}")
13911391
end
13921392

guard/test/guard/service_account/actions_test.exs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ defmodule Guard.ServiceAccount.ActionsTest do
9292
org_id: "org-id",
9393
creator_id: "creator-id",
9494
deactivated: false,
95-
email: "[email protected].#{Application.fetch_env!(:guard, :base_domain)}"
95+
email:
96+
"test@service_accounts.test-org.#{Application.fetch_env!(:guard, :base_domain)}"
9697
},
9798
api_token: "test-token"
9899
}}
@@ -102,7 +103,10 @@ defmodule Guard.ServiceAccount.ActionsTest do
102103
[
103104
create: fn user_id, email, name, "service_account" ->
104105
assert user_id == "user-id"
105-
assert email == "[email protected].#{Application.fetch_env!(:guard, :base_domain)}"
106+
107+
assert email ==
108+
"test@service_accounts.test-org.#{Application.fetch_env!(:guard, :base_domain)}"
109+
106110
assert name == "Test SA"
107111
:ok
108112
end,
@@ -120,7 +124,7 @@ defmodule Guard.ServiceAccount.ActionsTest do
120124
assert_called(
121125
Guard.Store.RbacUser.create(
122126
"user-id",
123-
"test@sa.test-org.#{Application.fetch_env!(:guard, :base_domain)}",
127+
"test@service_accounts.test-org.#{Application.fetch_env!(:guard, :base_domain)}",
124128
"Test SA",
125129
"service_account"
126130
)
@@ -394,7 +398,7 @@ defmodule Guard.ServiceAccount.ActionsTest do
394398

395399
assert String.contains?(
396400
service_account.email,
397-
"@sa.test-org.#{Application.fetch_env!(:guard, :base_domain)}"
401+
"@service_accounts.test-org.#{Application.fetch_env!(:guard, :base_domain)}"
398402
)
399403

400404
# Verify event was published

guard/test/guard/store/service_account_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ defmodule Guard.Store.ServiceAccountTest do
222222

223223
assert String.contains?(
224224
result.service_account.email,
225-
"@sa.test-org.#{Application.fetch_env!(:guard, :base_domain)}"
225+
"@service_accounts.test-org.#{Application.fetch_env!(:guard, :base_domain)}"
226226
)
227227
end
228228
end
@@ -263,7 +263,7 @@ defmodule Guard.Store.ServiceAccountTest do
263263

264264
# Should sanitize both name and org username
265265
assert result.service_account.email ==
266-
"my-service-account-@sa.myorg-123.#{Application.fetch_env!(:guard, :base_domain)}"
266+
"my-service-account-@service_accounts.myorg-123.#{Application.fetch_env!(:guard, :base_domain)}"
267267
end
268268
end
269269

@@ -280,7 +280,7 @@ defmodule Guard.Store.ServiceAccountTest do
280280
# Should use fallback email
281281
assert String.contains?(
282282
result.service_account.email,
283-
"@sa.unknown.#{Application.fetch_env!(:guard, :base_domain)}"
283+
"@service_accounts.unknown.#{Application.fetch_env!(:guard, :base_domain)}"
284284
)
285285
end
286286
end
@@ -337,7 +337,7 @@ defmodule Guard.Store.ServiceAccountTest do
337337

338338
assert String.contains?(
339339
updated_sa.user.email,
340-
"new-name@sa.test-org.#{Application.fetch_env!(:guard, :base_domain)}"
340+
"new-name@service_accounts.test-org.#{Application.fetch_env!(:guard, :base_domain)}"
341341
)
342342
end
343343
end

guard/test/guard/user/actions_test.exs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,17 @@ defmodule Guard.User.ActionsTest do
133133
describe "service account user interactions" do
134134
test "should not allow creating regular user with service account email pattern" do
135135
with_mock Guard.Events.UserCreated, publish: fn _, _ -> :ok end do
136+
base_domain = Application.fetch_env!(:guard, :base_domain)
137+
service_email = "test@service_accounts.org.#{base_domain}"
138+
136139
user_params = %{
137-
140+
email: service_email,
138141
name: "Regular User"
139142
}
140143

141144
{:ok, user} = Guard.User.Actions.create(user_params)
142145

143-
assert user.email == "[email protected]"
146+
assert user.email == service_email
144147
assert user.name == "Regular User"
145148
end
146149
end
@@ -161,7 +164,7 @@ defmodule Guard.User.ActionsTest do
161164
assert updated_user.name == "Updated SA Name"
162165
assert updated_user.creation_source == :service_account
163166
assert updated_user.single_org_user == true
164-
assert String.contains?(updated_user.email, "@sa.")
167+
assert String.contains?(updated_user.email, "@service_accounts.")
165168
end
166169
end
167170

guard/test/support/factories/service_account_factory.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ defmodule Support.Factories.ServiceAccountFactory do
109109

110110
defp generate_synthetic_email(name, _org_id) do
111111
sanitized_name = String.downcase(name) |> String.replace(~r/[^a-z0-9\-]/, "-")
112-
"#{sanitized_name}@sa.test-org.#{Application.fetch_env!(:guard, :base_domain)}"
112+
"#{sanitized_name}@service_accounts.test-org.#{Application.fetch_env!(:guard, :base_domain)}"
113113
end
114114

115115
defp get_role_id(nil), do: UUID.generate()

0 commit comments

Comments
 (0)