-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #224 from gaia-app/59-secure-api-state-endpoint
π : secure api state endpoint
- Loading branch information
Showing
19 changed files
with
501 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/io/codeka/gaia/config/security/StateApiSecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.codeka.gaia.config.security; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
import java.util.UUID; | ||
|
||
@Configuration | ||
@Order(69) | ||
public class StateApiSecurityConfig extends WebSecurityConfigurerAdapter { | ||
|
||
private PasswordEncoder bCrypt; | ||
|
||
private static final Log logger = LogFactory.getLog(StateApiSecurityConfig.class); | ||
|
||
@Autowired | ||
public StateApiSecurityConfig(PasswordEncoder bCrypt) { | ||
this.bCrypt = bCrypt; | ||
} | ||
|
||
@Override | ||
protected void configure(HttpSecurity http) throws Exception { | ||
http | ||
.antMatcher("/api/state/**") | ||
.authorizeRequests() | ||
.anyRequest().hasAnyRole("STATE", "USER") | ||
.and() | ||
.httpBasic(); | ||
} | ||
|
||
@Override | ||
public void configure(AuthenticationManagerBuilder auth) throws Exception { | ||
logger.info(String.format("%n%nUsing generated security password for state API: %s%n", properties().getPassword())); | ||
|
||
// configure default admin user | ||
auth | ||
.inMemoryAuthentication() | ||
.withUser(properties().getUsername()).password(bCrypt.encode(properties().getPassword())).authorities("ROLE_STATE"); | ||
} | ||
|
||
@Bean | ||
@ConfigurationProperties(prefix = "gaia.state.api") | ||
public StateApiSecurityProperties properties(){ | ||
return new StateApiSecurityProperties("gaia-backend", UUID.randomUUID().toString()); | ||
} | ||
|
||
public static class StateApiSecurityProperties { | ||
|
||
private String password; | ||
|
||
private String username; | ||
|
||
public StateApiSecurityProperties(String username, String password) { | ||
this.username = username; | ||
this.password = password; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/test/java/io/codeka/gaia/config/security/StateApiSecurityConfigIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.codeka.gaia.config.security; | ||
|
||
import io.codeka.gaia.test.MongoContainer; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.httpBasic; | ||
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.authenticated; | ||
import static org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers.unauthenticated; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
@DirtiesContext | ||
@Testcontainers | ||
public class StateApiSecurityConfigIT { | ||
|
||
@Container | ||
private static MongoContainer mongoContainer = new MongoContainer(); | ||
|
||
@Autowired | ||
private StateApiSecurityConfig.StateApiSecurityProperties props; | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Test | ||
void gaiaBackend_shouldHaveAccessToStateApi() throws Exception { | ||
mockMvc.perform(get("/api/state/test").with(httpBasic(props.getUsername(), props.getPassword()))) | ||
.andExpect(authenticated().withUsername("gaia-backend").withRoles("STATE")); | ||
} | ||
|
||
@Test | ||
void gaiaBackend_shouldNotHaveAccessToOtherApis() throws Exception { | ||
mockMvc.perform(get("/api/modules/test").with(httpBasic(props.getUsername(), props.getPassword()))) | ||
.andExpect(unauthenticated()); | ||
} | ||
|
||
@Test | ||
void gaiaBackend_shouldNotHaveAccessToScreens() throws Exception { | ||
mockMvc.perform(get("/modules/test").with(httpBasic(props.getUsername(), props.getPassword()))) | ||
.andExpect(unauthenticated()); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.codeka.gaia.e2e | ||
|
||
import org.openqa.selenium.WebDriver | ||
import org.openqa.selenium.WebElement | ||
import org.openqa.selenium.support.FindBy | ||
|
||
class JobPage(webDriver: WebDriver) { | ||
|
||
init { | ||
if(! webDriver.currentUrl.contains("/jobs/")){ | ||
throw IllegalStateException("This is not the job page. Current page is ${webDriver.currentUrl}") | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.codeka.gaia.e2e | ||
|
||
import org.openqa.selenium.WebDriver | ||
import org.openqa.selenium.WebElement | ||
import org.openqa.selenium.support.FindBy | ||
|
||
class StackPage(webDriver: WebDriver) { | ||
|
||
init { | ||
if(! webDriver.currentUrl.contains("/stacks/")){ | ||
throw IllegalStateException("This is not the stack page. Current page is ${webDriver.currentUrl}") | ||
} | ||
} | ||
|
||
@FindBy(id="stack.name") | ||
private lateinit var nameInput: WebElement | ||
|
||
fun stackName() : String = nameInput.getAttribute("value") | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.