Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# circleCI build file
version: 2.1


executors:
builder-large: # 4cpus 8gb ram
resource_class: 'large'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package yakity.security

import groovy.transform.CompileStatic

import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@CompileStatic
@RestController
@RequestMapping("/api/acl/test")
class DemoAclController {

@GetMapping("/")
def index() {
"get-check"
}

@PostMapping("/")
def save() {
"post-check"
}

@PutMapping("/")
def update() {
"put-check"
}

@DeleteMapping("/")
def delete() {
"delete-check"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,13 @@ import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.context.annotation.Lazy
import org.springframework.security.authentication.AuthenticationManager
import org.springframework.security.config.Customizer
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.security.web.SecurityFilterChain

import yakworks.security.audit.AuditStampConfiguration
import yakworks.security.gorm.SecurityGormConfiguration
import yakworks.security.spring.DefaultSecurityConfiguration
import yakworks.security.spring.PermissionsAuthorizationManager
import yakworks.security.spring.token.store.TokenStore

import static org.springframework.security.config.Customizer.withDefaults
Expand All @@ -51,14 +48,17 @@ class HelloSecurityConfiguration {

@Autowired(required = false) Saml2RelyingPartyProperties samlProps
@Autowired(required = false) TokenStore tokenStore;
@Autowired(required = false) UserDetailsService userDetailsService

@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
SecurityFilterChain securityFilterChain(HttpSecurity http, PermissionsAuthorizationManager permissionsAuthorizationManager) throws Exception {
// DefaultSecurityConfiguration.applyBasicDefaults(http)
http
.authorizeHttpRequests((authorize) -> authorize
.requestMatchers("/actuator/**", "/resources/**", "/about").permitAll()
.requestMatchers("/api/**").access(permissionsAuthorizationManager)
.anyRequest().authenticated()

)
// enable basic auth
.httpBasic(withDefaults())
Expand All @@ -73,7 +73,7 @@ class HelloSecurityConfiguration {
}

DefaultSecurityConfiguration.addJsonAuthenticationFilter(http, tokenStore);
DefaultSecurityConfiguration.applyOauthJwt(http);
DefaultSecurityConfiguration.applyOauthJwt(http, userDetailsService);

return http.build()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package yakity.security

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.security.test.context.support.WithMockUser
import org.springframework.test.web.servlet.MockMvc
import spock.lang.Specification

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

@SpringBootTest(classes = [TestSpringApplication])
@AutoConfigureMockMvc
class AclPermissionSpec extends Specification {

@Autowired MockMvc mockMvc

void "should be unauthroized"() {
expect:
// status.value(), result.getResponse().getStatus());
this.mockMvc.perform(get("/api/acl/test/1")).andExpect(status().isUnauthorized())
this.mockMvc.perform(post( "/api/acl/test/")).andExpect(status().isUnauthorized())
this.mockMvc.perform(put( "/api/acl/test/")).andExpect(status().isUnauthorized())
this.mockMvc.perform(delete( "/api/acl/test/1")).andExpect(status().isUnauthorized())
}

//user has acl:foo:* but not acl:test:*
@WithMockUser(authorities = ['acl:foo:*'])
void "should be forbidden"() {
expect:
// status.value(), result.getResponse().getStatus());
this.mockMvc.perform(get("/api/acl/test/1")).andExpect(status().isForbidden())
this.mockMvc.perform(post( "/api/acl/test/")).andExpect(status().isForbidden())
this.mockMvc.perform(put( "/api/acl/test/")).andExpect(status().isForbidden())
this.mockMvc.perform(delete( "/api/acl/test/1")).andExpect(status().isForbidden())
}

@WithMockUser(authorities = ['acl:test:read'])
void "read check"() {
expect:
this.mockMvc.perform(get("/api/acl/test/")).andExpect(status().isOk());
}

@WithMockUser(authorities = ['acl:test:create'])
void "post check"() {
expect:
this.mockMvc.perform(post("/api/acl/test/")).andExpect(status().isOk());
}

@WithMockUser(authorities = ['acl:test:update'])
void "put check"() {
expect:
this.mockMvc.perform(put("/api/acl/test/")).andExpect(status().isOk());
}

@WithMockUser(authorities = ['acl:test:delete'])
void "delete check"() {
expect:
this.mockMvc.perform(delete("/api/acl/test/")).andExpect(status().isOk());
}

@WithMockUser(authorities = ['acl:test:*'])
void "wildcard check"() {
expect:
this.mockMvc.perform(get("/api/acl/test/")).andExpect(status().isOk());
this.mockMvc.perform(post("/api/acl/test/")).andExpect(status().isOk());
this.mockMvc.perform(put("/api/acl/test/")).andExpect(status().isOk());
this.mockMvc.perform(delete("/api/acl/test/")).andExpect(status().isOk());
}

@WithMockUser(authorities = ['acl:*:*'])
void "wildcard check 2"() {
expect:
this.mockMvc.perform(get("/api/acl/test/")).andExpect(status().isOk());
this.mockMvc.perform(post("/api/acl/test/")).andExpect(status().isOk());
this.mockMvc.perform(put("/api/acl/test/")).andExpect(status().isOk());
this.mockMvc.perform(delete("/api/acl/test/")).andExpect(status().isOk());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class UrlMappings {
//to test errors and error handlers
post "/api/rally/exceptionTest/runtimeException"(controller: 'exceptionTest', action:'runtimeException', namespace:'rally')
post "/api/rally/exceptionTest/throwable"(controller: 'exceptionTest', action:'throwable', namespace:'rally')

post "/rally/org/rpc"(controller: 'org', action: 'rpc')
}

static void runClosure(Closure mappingClosure, Object delegate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ class OrgController implements CrudApiController<Org> {
respondWith(entityMap, [status: CREATED, params: qParams])
}

//just here to hit rpc during tests
def rpc(){
Map qParams = getParamsMap()
String op = qParams['op']
respond([ok:true, rpc:op])
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package yakworks

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import org.springframework.web.bind.support.DefaultDataBinderFactory

import grails.gorm.transactions.Rollback
import grails.testing.mixin.integration.Integration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import yakworks.commons.io.ZipUtils
import yakworks.commons.util.BuildSupport

import yakworks.rally.attachment.model.Attachment
import yakworks.rally.attachment.repo.AttachmentRepo
import yakworks.rally.job.SyncJob
import yakworks.rally.orgs.model.Contact
import yakworks.rest.gorm.controller.CrudApiController
Expand All @@ -22,7 +21,6 @@ import yakworks.testing.rest.RestIntTest
class BulkCsvSpec extends RestIntTest implements SecuritySpecHelper {

CrudApiController<Contact> controller
AttachmentRepo attachmentRepo

void setup() {
controllerName = 'ContactController'
Expand Down Expand Up @@ -91,10 +89,8 @@ class BulkCsvSpec extends RestIntTest implements SecuritySpecHelper {
syncJob.dataId != null //should have been set for bulk csv.

cleanup: "cleanup db"
attachmentRepo.removeById(syncJob.dataId as Long)
if(body.id) SyncJob.repo.removeById(body.id as Long)
Attachment.withNewTransaction {
if(attachment) attachment.remove()
if(body.id) SyncJob.repo.removeById(body.id as Long) //syncjob is created in new transaction
Contact.findAllByNumLike("bulk_").each {
it.remove()
}
Expand Down Expand Up @@ -155,17 +151,14 @@ class BulkCsvSpec extends RestIntTest implements SecuritySpecHelper {
syncJob.dataId != null //should have been set for bulk csv.

cleanup: "cleanup db"
attachmentRepo.removeById(syncJob.dataId as Long)
if(body.id) SyncJob.repo.removeById(body.id as Long)
Attachment.withNewTransaction {
if(attachment) attachment.remove()
if(body.id) SyncJob.repo.removeById(body.id as Long) //syncjob is created in new transaction
Contact.findAllByNumLike("bulk_").each {
it.remove()
}
}
}


void "test bulk update with csv"() {

expect:
Expand Down Expand Up @@ -209,10 +202,7 @@ class BulkCsvSpec extends RestIntTest implements SecuritySpecHelper {
c11.lastName == "test" //should have been updated

cleanup: "cleanup db"
Attachment.withNewTransaction {
if(attachment) attachment.remove()
if(body.id) SyncJob.repo.removeById(body.id as Long) //syncjob is created in new transaction
}
if(body.id) SyncJob.repo.removeById(body.id as Long)
}

void "test bad CSV"() {
Expand Down Expand Up @@ -240,7 +230,6 @@ class BulkCsvSpec extends RestIntTest implements SecuritySpecHelper {
body.detail.contains "Error on record number 2"

cleanup: "cleanup db"
//attachmentRepo.removeById(attachment.id)
Attachment.withNewTransaction {
if(attachment) attachment.remove()
//if(body.id) SyncJob.repo.removeById(body.id as Long) //syncjob is created in new transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import static yakworks.json.groovy.JsonEngine.parseJson
@Rollback
class BulkRestApiSpec extends Specification implements OkHttpRestTrait {

//queue is not configured for test env yet
//@Autowired BlockingQueue<SyncJob> syncJobQueue

String path = "/api/rally/org/bulk?jobSource=Oracle&async=false"

def setup(){
Expand Down Expand Up @@ -160,6 +157,13 @@ class BulkRestApiSpec extends Specification implements OkHttpRestTrait {
json[1].data.name == "Foox2"
json[1].data.source.sourceId == "Foox2"

when: "verify problems are stored in problems field too for bulk"
List problems = job.problems

then: "should pickup problems from data"
problems.size() == 1
problems[0].payload.name == "Foox1"

delete("/api/rally/org", json.data[1].id)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package yakworks.rest

import org.springframework.http.HttpStatus


import yakworks.rest.client.OkHttpRestTrait
import grails.testing.mixin.integration.Integration
import okhttp3.Response
Expand All @@ -15,7 +13,7 @@ class ExKitchenSinkApiSpec extends Specification implements OkHttpRestTrait {

String path = "/api/kitchen"

def setup(){
void setup(){
login()
}

Expand All @@ -31,7 +29,6 @@ class ExKitchenSinkApiSpec extends Specification implements OkHttpRestTrait {
// bodyText == "123"
body.id
body.name == 'Sink1'

}

void "testing post"() {
Expand All @@ -49,7 +46,6 @@ class ExKitchenSinkApiSpec extends Specification implements OkHttpRestTrait {

void "post with bindId"() {
when:

Response resp = post(path + "?bindId=true", [num: "foobie123", name: "foobie", id:9999])
Map body = bodyToMap(resp)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package yakworks.rest
import grails.gorm.transactions.Rollback
import grails.testing.mixin.integration.Integration
import okhttp3.Response
import spock.lang.Ignore
import spock.lang.Specification

import yakworks.rally.orgs.model.Org
Expand All @@ -18,6 +19,11 @@ class RestApiQueryValidationSpec extends Specification implements OkHttpRestTrai
login()
}

void cleanupSpec() {
OkAuth.TOKEN = null
}

@Ignore("FIXME @SUD, this user now doesnt have permission to read orgs")
@Rollback
void "test list - non admin user"() {
setup: "this user cant max > 50"
Expand Down
Loading