Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BENCH-4609] Add a new Utility method isServiceAccount #209

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/main/java/bio/terra/common/iam/IamUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package bio.terra.common.iam;

public final class IamUtils {

/**
* Check whether the provided email address is a service account.
*
* @param email
* @return true if the email address is a service account
*/
public static boolean isServiceAccount(String email) {
if (email == null) {
return false;
}
return email.matches("\\S+@\\S*gserviceaccount\\.com$");
}

}
33 changes: 33 additions & 0 deletions src/test/java/bio/terra/common/iam/IamUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package bio.terra.common.iam;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class IamUtilsTest {

@Test
public void isServiceAccountWithValidServiceAccountEmail() {
assertTrue(IamUtils.isServiceAccount("user@gserviceaccount.com"));
}

@Test
public void isServiceAccountWithInvalidServiceAccountEmail() {
assertFalse(IamUtils.isServiceAccount("user@verily.com"));
}

@Test
public void isServiceAccountWithWhitespaceInEmail() {
assertFalse(IamUtils.isServiceAccount(" user@gserviceaccount.com"));
}

@Test
public void isServiceAccountWithEmptyEmail() {
assertFalse(IamUtils.isServiceAccount(""));
}

@Test
public void isServiceAccountWithNullEmail() {
assertFalse(IamUtils.isServiceAccount(null));
}
}
Loading