Skip to content

Commit 5f7aca2

Browse files
Validate group inputs on invites (#1439)
1 parent 3c68779 commit 5f7aca2

File tree

5 files changed

+58
-25
lines changed

5 files changed

+58
-25
lines changed

Dockerfile

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,39 +42,40 @@ FROM alpine:3.17.0 as tools
4242
ARG TARGETARCH
4343

4444
# renovate: datasource=github-releases depName=helm/helm
45-
ENV HELM_VERSION=v3.11.0
45+
ENV HELM_VERSION=v3.17.3
4646

4747
# renovate: datasource=github-releases depName=alco/goon
4848
ENV GOON_VERSION=v1.1.1
4949

5050
# renovate: datasource=github-releases depName=pluralsh/plural-cli
51-
ENV CLI_VERSION=v0.7.8
52-
53-
# renovate: datasource=github-releases depName=accurics/terrascan
54-
ENV TERRASCAN_VERSION=v1.17.1
51+
ENV CLI_VERSION=v0.12.8
5552

5653
# renovate: datasource=github-releases depName=aquasecurity/trivy
57-
ENV TRIVY_VERSION=v0.36.1
54+
ENV TRIVY_VERSION=v0.64.1
5855

5956
RUN apk add --update --no-cache curl ca-certificates unzip wget openssl && \
6057
# download helm
61-
curl -L https://get.helm.sh/helm-${HELM_VERSION}-linux-${TARGETARCH}.tar.gz | tar xvz && \
58+
echo "installing helm" && \
59+
curl -L https://get.helm.sh/helm-${HELM_VERSION}-linux-${TARGETARCH}.tar.gz | tar xz && \
6260
mv linux-${TARGETARCH}/helm /usr/local/bin/helm && \
6361
# download goon
64-
curl -L https://github.com/alco/goon/releases/download/${GOON_VERSION}/goon_linux_${TARGETARCH}.tar.gz | tar xvz && \
65-
mv goon /usr/local/bin/goon && \
62+
# echo "installing goon" && \
63+
# curl -L https://github.com/alco/goon/releases/download/${GOON_VERSION}/goon_linux_${TARGETARCH}.tar.gz | tar xvz && \
64+
# mv goon /usr/local/bin/goon && \
6665
# download plural cli
67-
curl -L https://github.com/pluralsh/plural-cli/releases/download/${CLI_VERSION}/plural-cli_console_${CLI_VERSION/v/}_Linux_${TARGETARCH}.tar.gz | tar xvz plural && \
66+
echo "installing plural" && \
67+
curl -L https://github.com/pluralsh/plural-cli/releases/download/${CLI_VERSION}/plural-cli_${CLI_VERSION#v}_Linux_${TARGETARCH}.tar.gz | tar xvz plural && \
6868
mv plural /usr/local/bin/plural && \
6969
# download terrascan
70-
if [ "$TARGETARCH" = "amd64" ]; then \
71-
curl -L https://github.com/accurics/terrascan/releases/download/${TERRASCAN_VERSION}/terrascan_${TERRASCAN_VERSION/v/}_Linux_x86_64.tar.gz > terrascan.tar.gz; \
72-
else \
73-
curl -L https://github.com/accurics/terrascan/releases/download/${TERRASCAN_VERSION}/terrascan_${TERRASCAN_VERSION/v/}_Linux_${TARGETARCH}.tar.gz > terrascan.tar.gz; \
74-
fi && \
75-
tar -xf terrascan.tar.gz terrascan && rm terrascan.tar.gz && \
76-
mv terrascan /usr/local/bin/terrascan && \
70+
# if [ "$TARGETARCH" = "amd64" ]; then \
71+
# curl -L https://github.com/accurics/terrascan/releases/download/${TERRASCAN_VERSION}/terrascan_${TERRASCAN_VERSION/v/}_Linux_x86_64.tar.gz > terrascan.tar.gz; \
72+
# else \
73+
# curl -L https://github.com/accurics/terrascan/releases/download/${TERRASCAN_VERSION}/terrascan_${TERRASCAN_VERSION/v/}_Linux_${TARGETARCH}.tar.gz > terrascan.tar.gz; \
74+
# fi && \
75+
# tar -xf terrascan.tar.gz terrascan && rm terrascan.tar.gz && \
76+
# mv terrascan /usr/local/bin/terrascan && \
7777
# download trivy
78+
echo "installing trivy" && \
7879
if [ "$TARGETARCH" = "amd64" ]; then \
7980
curl -L https://github.com/aquasecurity/trivy/releases/download/${TRIVY_VERSION}/trivy_${TRIVY_VERSION/v/}_Linux-64bit.tar.gz > trivy.tar.gz; \
8081
elif [ "$TARGETARCH" = "arm64" ]; then \
@@ -84,9 +85,9 @@ RUN apk add --update --no-cache curl ca-certificates unzip wget openssl && \
8485
mv trivy /usr/local/bin/trivy && \
8586
# make tools executable
8687
chmod +x /usr/local/bin/helm && \
87-
chmod +x /usr/local/bin/goon && \
88+
# chmod +x /usr/local/bin/goon && \
8889
chmod +x /usr/local/bin/plural && \
89-
chmod +x /usr/local/bin/terrascan && \
90+
# chmod +x /usr/local/bin/terrascan && \
9091
chmod +x /usr/local/bin/trivy
9192

9293
FROM erlang:24.3.4.6-alpine
@@ -112,8 +113,8 @@ WORKDIR /opt/app
112113

113114
COPY --from=tools /usr/local/bin/plural /usr/local/bin/plural
114115
COPY --from=tools /usr/local/bin/helm /usr/local/bin/helm
115-
COPY --from=tools /usr/local/bin/goon /usr/local/bin/goon
116-
COPY --from=tools /usr/local/bin/terrascan /usr/local/bin/terrascan
116+
# COPY --from=tools /usr/local/bin/goon /usr/local/bin/goon
117+
# COPY --from=tools /usr/local/bin/terrascan /usr/local/bin/terrascan
117118
COPY --from=tools /usr/local/bin/trivy /usr/local/bin/trivy
118119
COPY --from=builder /opt/built .
119120

apps/core/lib/core/services/accounts.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@ defmodule Core.Services.Accounts do
290290
|> allow(user, :create)
291291
|> when_ok(:insert)
292292
end)
293+
|> add_operation(:validate, fn %{invite: invite} ->
294+
with %Invite{groups: [_ | _] = groups} <- Core.Repo.preload(invite, [:groups]),
295+
true <- Enum.any?(groups, & &1.account_id != aid) do
296+
{:error, "you cannot invite users to groups in other accounts"}
297+
else
298+
_ -> {:ok, invite}
299+
end
300+
end)
293301
|> execute(extract: :invite)
294302
|> notify(:create, user)
295303
end

apps/core/lib/core/services/base.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule Core.Services.Base do
1818
_ -> []
1919
end
2020
end
21+
def find_bindings(%User{id: id}), do: [%{user_id: id}]
2122
def find_bindings(_), do: []
2223

2324
def ok(val), do: {:ok, val}

apps/core/test/services/accounts_test.exs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,29 @@ defmodule Core.Services.AccountsTest do
338338
assert invite.account_id == account.id
339339
end
340340

341+
test "you can invite users into an accounts groups", %{user: user, account: account} do
342+
group = insert(:group, account: account)
343+
{:ok, invite} = Accounts.create_invite(%{
344+
345+
invite_groups: [%{group_id: group.id}]
346+
}, user)
347+
348+
assert invite.email == "[email protected]"
349+
assert invite.secure_id
350+
assert invite.account_id == account.id
351+
352+
%{groups: [%{id: id}]} = Core.Repo.preload(invite, [:groups])
353+
assert id == group.id
354+
end
355+
356+
test "you cannot invite users into another account's groups", %{user: user} do
357+
group = insert(:group)
358+
{:error, _} = Accounts.create_invite(%{
359+
360+
invite_groups: [%{group_id: group.id}]
361+
}, user)
362+
end
363+
341364
test "it will not accept invalid emails", %{user: user} do
342365
{:error, _} = Accounts.create_invite(%{email: "invalidemail"}, user)
343366
end

apps/graphql/test/mutations/repository_mutation_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ defmodule GraphQl.RepositoryMutationsTest do
337337
assert provider["redirectUris"] == ["example.com"]
338338
assert provider["configuration"]["issuer"] == "https://oidc.plural.sh/"
339339

340-
[%{"group" => g}] = provider["bindings"]
341-
assert g["id"] == group.id
340+
assert Enum.any?(provider["bindings"], & get_in(&1, ["group", "id"]) == group.id)
341+
assert Enum.any?(provider["bindings"], & get_in(&1, ["user", "id"]) == installation.user.id)
342342
end
343343

344344
test "it will create an user-bound oidc provider" do
@@ -385,8 +385,8 @@ defmodule GraphQl.RepositoryMutationsTest do
385385
assert provider["redirectUris"] == ["example.com"]
386386
assert provider["configuration"]["issuer"] == "https://oidc.plural.sh/"
387387

388-
[%{"group" => g}] = provider["bindings"]
389-
assert g["id"] == group.id
388+
assert Enum.any?(provider["bindings"], & get_in(&1, ["group", "id"]) == group.id)
389+
assert Enum.any?(provider["bindings"], & get_in(&1, ["user", "id"]) == user.id)
390390
end
391391
end
392392

0 commit comments

Comments
 (0)