Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.avioconsulting.mule.deployment.dsl.policies

import com.avioconsulting.mule.deployment.api.models.policies.ClientEnforcementPolicyCustom
import com.avioconsulting.mule.deployment.dsl.BaseContext

class ClientEnforcementPolicyCustomContext extends BaseContext {
String version
private PathsContext paths = new PathsContext()
private boolean pathsCalled = false
String clientIdExpression
String clientSecretExpression

ClientEnforcementPolicyCustom createPolicyModel() {
def pathListing = paths.createModel()
if (pathsCalled && !pathListing.any()) {
throw new Exception("You specified 'paths' but did not supply any 'path' declarations inside it. Either remove the paths declaration (policy applies to all resources) or declare one.")
}
new ClientEnforcementPolicyCustom( pathListing,
this.clientIdExpression, this.clientSecretExpression,
this.version)
}

def invokeMethod(String name, def args) {
if (name == 'paths') {
pathsCalled = true
}
super.invokeMethod(name,
args)
}

@Override
List<String> findOptionalProperties() {
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ class PolicyListContext {
policies << policyContext.createPolicyModel()
}

def clientEnforcementPolicyCustom(Closure closure = {}) {
def policyContext = new ClientEnforcementPolicyCustomContext()
closure.delegate = policyContext
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure.call()
policies << policyContext.createPolicyModel()
}

def mulesoftPolicy(Closure closure) {
def policyContext = new PolicyContext(Policy.getMulesoftGroupId())
closure.delegate = policyContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,55 @@ class PolicyContextTest {
],
'1.4.1')))
}

@Test
void client_enforcement_client_Id_secret_minimum() {
// arrange
def context = new ClientEnforcementPolicyCustomContext()
def closure = {
clientSecretExpression "#[attributes.headers['client_secret']]"
}
closure.delegate = context
closure.call()

// act
def request = context.createPolicyModel()
// assert
assertThat request,
is(equalTo(new ClientEnforcementPolicyCustom()))
}

@Test
void client_enforcement_client_Id_secret_full() {
// arrange
def context = new ClientEnforcementPolicyCustomContext()
println(context)
def closure = {
version '1.4.1'
paths {
path {
method HttpMethod().put
regex '.*bar'
}
}
clientIdExpression "#[attributes.headers['client_id']]"
clientSecretExpression "#[attributes.headers['client_secret']]"

}
closure.delegate = context
closure.call()

// act
def request = context.createPolicyModel()

// assert
assertThat request,
is(equalTo(new ClientEnforcementPolicyCustom([
new PolicyPathApplication([HttpMethod.PUT],
'.*bar')
],
"#[attributes.headers['client_id']]",
"#[attributes.headers['client_secret']]",
'1.4.1')))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ class PolicyListContextTest {
is(instanceOf(ClientEnforcementPolicyBasicAuth))
}

@Test
void clientEnforcementPolicyCustom_with_details() {
// arrange
def context = new PolicyListContext()
def closure = {
policy {
assetId 'the-asset-id'
version '1.2.1'
config hello: 'there'
}
clientEnforcementPolicyCustom {
clientSecretExpression "#[attributes.headers['client_secret']]"
}
}
closure.delegate = context
closure.call()

// act
def result = context.createPolicyList()

// assert
assertThat result.size(),
is(equalTo(2))
assertThat result[0],
is(instanceOf(Policy))
assertThat result[1],
is(instanceOf(ClientEnforcementPolicyCustom))
}

@Test
void no_policies_on_purpose() {
// arrange
Expand Down