Skip to content

Commit fa7415c

Browse files
author
Charles Daniels
committed
correct SDK example usage
1 parent 07e82b9 commit fa7415c

File tree

10 files changed

+80
-18
lines changed

10 files changed

+80
-18
lines changed
Binary file not shown.
Binary file not shown.

.gradle/8.7/fileHashes/fileHashes.bin

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
306 Bytes
Binary file not shown.
Binary file not shown.

.gradle/file-system.probe

0 Bytes
Binary file not shown.

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ You can use the Styra OPA-SpringBoot SDK to connect [Open Policy Agent](https://
1111

1212
This package is published on Maven Central as TODO. The Maven Central page includes up-to-date instructions to add it as a dependency to your Java project, tailored to a variety of build systems including Maven and Gradle.
1313

14-
If you wish to build from source and publish the SDK artifact to your local Maven repository (on your filesystem) then use the following command (after cloing the git repo locally):
14+
If you wish to build from source and publish the SDK artifact to your local Maven repository (on your filesystem) then use the following command (after cloning the git repo locally):
1515

1616
On Linux/MacOS:
1717

@@ -27,26 +27,26 @@ gradlew.bat publishToMavenLocal -Pskip.signing
2727

2828
## SDK Example Usage (high-level)
2929

30-
TODO
3130

3231
```java
33-
import TODO.OPAAuthorizationManager;
32+
import com.styra.opa.springboot.OPAAuthorizationManager;
33+
import com.styra.opa.OPAClient;
3434

3535
@Configuration
3636
@EnableWebSecurity
3737
public class SecurityConfig {
3838

39+
@Autowired
40+
TicketRepository ticketRepository;
41+
42+
@Autowired
43+
TenantRepository tenantRepository;
44+
45+
@Autowired
46+
CustomerRepository customerRepository;
47+
3948
@Bean
4049
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
41-
// NOTE: The `.csrf(...)` disables CSRF protections. This could
42-
// be a serious security vulnerability in a production environment.
43-
// However, since this API is intended for educational and development
44-
// purposes, it is disabled because it makes it easier to work with
45-
// locally. If you want to use any of this code for a production
46-
// service, it is important to re-enable CSRF protection.
47-
//http.authorizeHttpRequests(authorize -> authorize
48-
// .anyRequest().access(customAuthManager())).csrf(csrf -> csrf.disable());
49-
5050

5151
String opaURL = "http://localhost:8181";
5252
String opaURLEnv = System.getenv("OPA_URL");
@@ -55,14 +55,15 @@ public class SecurityConfig {
5555
}
5656
OPAClient opa = new OPAClient(opaURL);
5757

58-
http.authorizeHttpRequests(authorize -> authorize
59-
.anyRequest().access(new OPAAuthorizationManager(opa))).csrf(csrf -> csrf.disable());
58+
AuthorizationManager<RequestAuthorizationContext> am = new OPAAuthorizationManager(opa, "tickets/spring/main");
6059

61-
return http.build();
60+
http.authorizeHttpRequests(authorize -> authorize.anyRequest().access(am));
6261

62+
return http.build();
6363
}
6464

6565
}
66+
6667
```
6768

6869
## Policy Inputs & Outputs
@@ -94,7 +95,7 @@ In order to make OPA-SpringBoot compatible with [AuthZEN](https://openid.github.
9495
| `output.context.id` | AuthZEN [Reason Object](https://openid.github.io/authzen/#name-reason-object) ID |
9596
| `output.context.reason_admin` | AuthZEN [Reason Field Object](https://openid.github.io/authzen/#reason-field), for administrative use |
9697
| `output.context.reason_user` | AuthZEN [Reason Field Object](https://openid.github.io/authzen/#reason-field), for user-facing error messages |
97-
| `output.context.data` | Optional supplemental data provided by your OPA policy.
98+
| `output.context.data` | Optional supplemental data provided by your OPA policy |
9899

99100
### Build Instructions
100101

build.gradle

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
plugins {
22
id 'java'
3+
id 'maven-publish'
4+
id 'signing'
35
id 'org.springframework.boot' version '3.3.0'
46
id 'io.spring.dependency-management' version '1.1.4'
57
id("checkstyle")
@@ -55,7 +57,7 @@ dependencies {
5557
}
5658

5759
apply plugin: 'application'
58-
mainClassName = 'com.styra.opa_springboot'
60+
mainClassName = 'com.styra.opa.springboot'
5961

6062
tasks.named('test') {
6163
useJUnitPlatform()
@@ -97,3 +99,62 @@ gradle.projectsEvaluated {
9799
}
98100
}
99101

102+
103+
publishing {
104+
repositories {
105+
maven {
106+
name = "OSSRH"
107+
url = 'https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/'
108+
credentials {
109+
username = System.getenv("MAVEN_USERNAME")
110+
password = System.getenv("MAVEN_PASSWORD")
111+
}
112+
}
113+
}
114+
115+
publications {
116+
maven(MavenPublication) {
117+
groupId = 'com.styra.opa'
118+
artifactId = 'springboot'
119+
version = '0.0.1'
120+
121+
from components.java
122+
123+
pom {
124+
name = 'Styra Spring Boot SDK'
125+
description = 'SDK enabling Spring Boot developers to easily integrate with the Styra API.'
126+
url = 'https://github.com/styrainc/opa-springboot'
127+
scm {
128+
url = 'github.com/styrainc/opa-springboot'
129+
connection = 'scm:git:ssh://git@github.com/styrainc/opa-springboot.git'
130+
}
131+
licenses { // TODO: should be apache 2
132+
license {
133+
name = 'The MIT License (MIT)'
134+
url = 'https://mit-license.org/'
135+
}
136+
}
137+
developers {
138+
developer {
139+
name = 'Styra'
140+
organization = 'Styra'
141+
email = 'devrel@styra.com'
142+
}
143+
}
144+
organization {
145+
name = 'Styra'
146+
url = 'www.styra.com'
147+
}
148+
}
149+
}
150+
}
151+
}
152+
153+
if (!project.hasProperty('skip.signing')) {
154+
signing {
155+
def signingKey = findProperty("signingKey")
156+
def signingPassphrase = findProperty("signingPassphrase")
157+
useInMemoryPgpKeys(signingKey, signingPassphrase)
158+
sign publishing.publications.maven
159+
}
160+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = 'opa_springboot'
1+
rootProject.name = 'springboot'

0 commit comments

Comments
 (0)