Skip to content

Commit 2d6644c

Browse files
authored
Merge pull request #264 from AtlasOfLivingAustralia/236-remove-api-key
#236 remove API keys
2 parents 9384a1b + 54d6b52 commit 2d6644c

16 files changed

+101
-185
lines changed

README.md

-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ The default develop url is http://devt.ala.org.au:8080/ws
3838

3939
#### Minimum configurations in external config file:
4040

41-
api_key: xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx
4241
google:
4342
apikey: xxxxxxxxxxxxxx
4443

@@ -50,7 +49,6 @@ The default develop url is http://devt.ala.org.au:8080/ws
5049
google:
5150
apikey: "xxxxxxxxxxxx"
5251

53-
api_key: xxxxxxxxxxxxx
5452
spatialHubUrl: https://spatial-test.ala.org.au/
5553

5654
geoserver:
@@ -70,9 +68,6 @@ The default develop url is http://devt.ala.org.au:8080/ws
7068
shp2pgsql.path: "/usr/bin/shp2pgsql"
7169
gdal.dir: "/usr/bin/"
7270

73-
slaveKey: "xxxxxxxxxxxxxx"
74-
serviceKey: "xxxxxxxxxxxxxx"
75-
7671
layers_store.GEONETWORK_URL: 'https://spatial-test.ala.org.au/geonetwork'
7772

7873
# Installation

grails-app/conf/application.yml

+2-21
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,9 @@ biocacheUrl: 'https://biocache.ala.org.au'
139139
openstreetmap:
140140
url: 'https://spatial.ala.org.au/osm'
141141

142-
slave.enable: true
143-
service.enable: true
144-
145142
spatialService:
146143
url: "https://spatial.ala.org.au/ws"
147144

148-
serviceKey: ""
149145
batch_sampling_passwords: ''
150146
batch_sampling_points_limit: 1000000
151147
batch_sampling_fields_limit: 1000
@@ -154,9 +150,6 @@ grid_buffer_size: 40960
154150
occurrence_species_records_filename: /data/ala/data/layers/process/density/current/records
155151

156152
---
157-
#
158-
# au.org.ala.spatial.slave config
159-
#
160153
spatialService.url: "http://localhost:8080/ws"
161154
data.dir: "/data/spatial-data"
162155
shp2pgsql.path: "/usr/bin/shp2pgsql"
@@ -181,9 +174,6 @@ maxent.threads: 4
181174

182175
sampling.threads: 4
183176

184-
slaveKey: ""
185-
serviceKey: ""
186-
187177
# time between pushing status updates to the master for a task
188178
statusTime: 5000
189179
retryCount: 10
@@ -237,20 +227,12 @@ security:
237227
core:
238228
roleAttribute: ala:role
239229
apikey:
240-
enabled: true
230+
enabled: false
241231
auth:
242232
serviceUrl: https://auth-test.ala.org.au/apikey/
243-
check:
244-
serviceUrl: https://auth-test.ala.org.au/apikey/ws/check?apikey=
245233
userdetails:
246234
serviceUrl: https://auth-test.ala.org.au/userdetails/
247235

248-
#webservice:
249-
# jwt: true
250-
# jwt-scopes: openid users/read
251-
# client-id: second-client-id-for-machine-to-machine-comms
252-
# client-secret: second-client-secret-for-machine-to-machine-comms
253-
254236
auth.admin_role: "ROLE_ADMIN"
255237
app.http.header.userId: "X-ALA-userId"
256238

@@ -264,7 +246,6 @@ namematching.url: 'https://namematching-ws.ala.org.au'
264246

265247
records.url: 'https://archives.ala.org.au/archives/exports/lat_lon_taxon.zip'
266248

267-
api_key: ''
268249
lists.url: 'https://lists.ala.org.au'
269250
collections.url: 'https://collections.ala.org.au'
270251
sandboxHubUrl: 'https://sandbox.ala.org.au/ala-hub'
@@ -402,7 +383,7 @@ environments:
402383
dir: "/Library/Frameworks/GDAL.framework/Programs"
403384

404385
spatialHubUrl: "http://devt.ala.org.au:8079/"
405-
# Slave config
386+
406387
spatialService:
407388
url: "http://devt.ala.org.au:8081/ws"
408389
remote: "https://spatial-test.ala.org.au/ws"

grails-app/controllers/au/org/ala/spatial/LogController.groovy

+9-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
package au.org.ala.spatial
1717

18-
18+
import au.ala.org.ws.security.RequireApiKey
1919
import au.org.ala.plugins.openapi.Path
2020
import au.org.ala.web.AuthService
2121
import com.opencsv.CSVWriter
@@ -64,18 +64,22 @@ class LogController {
6464
)
6565
@Path("/log")
6666
@Transactional
67+
@RequireApiKey
6768
def index() {
69+
Map emptyMap = [:]
6870
try {
6971
def lg = new Log(request.JSON as Map)
7072
if (!lg.save()) {
7173
lg.errors.each {
7274
log.error(it)
7375
}
7476
}
75-
render status: 200
77+
response.status = 200
78+
render emptyMap as JSON
7679
} catch (Exception e) {
7780
log.warn("log info is broken, ignored! " + e.getMessage())
78-
render status: 400
81+
response.status = 400
82+
render emptyMap as JSON
7983
}
8084
}
8185

@@ -129,7 +133,7 @@ class LogController {
129133
@Parameter(
130134
name = "admin",
131135
in = QUERY,
132-
description = "When true, return results for all users if request has a valid api_key or admin role.",
136+
description = "When true, return results for all users if request has an admin role.",
133137
required = false,
134138
example = "true"
135139
),
@@ -188,6 +192,7 @@ class LogController {
188192
)
189193
@Path("/log/search")
190194
@Produces("application/json")
195+
@RequireApiKey
191196
def search() {
192197
def searchResult = logService.search(params, authService.getUserId(), spatialAuthService.userInRole(spatialConfig.auth.admin_role))
193198
def totalCount = logService.searchCount(params, authService.getUserId(), spatialAuthService.userInRole(spatialConfig.auth.admin_role))
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,12 @@
11
package au.org.ala.spatial
22

3-
import au.org.ala.spatial.RequireAdmin
4-
import au.org.ala.spatial.RequireLogin
5-
import au.org.ala.spatial.RequirePermission
6-
import au.org.ala.spatial.SkipSecurityCheck
7-
import au.org.ala.spatial.SpatialConfig
83
import au.org.ala.web.AuthService
94
import com.google.common.base.Strings
105
import grails.converters.JSON
116

12-
/**
13-
* Copy and simplify from plugin "ala-ws-security-plugin:2.0"
14-
* Remove IP/method/controller whitelist support
15-
* collecting api_key from POST body (backward compatiblity support)
16-
*
17-
* TODO use ALA standard apiKey method: store apiKey in hearder
18-
*/
19-
import org.grails.web.util.WebUtils
20-
21-
import java.text.MessageFormat
22-
237
class LoginInterceptor {
248
static final int STATUS_UNAUTHORISED = 401
259
static final int STATUS_FORBIDDEN = 403
26-
static final String[] USERID_HEADER_NAME = ["X-ALA-userId", "userId", "user_id"]
27-
static final String[] API_KEY_HEADER_NAME = ["apiKey", "api_key", "api-key"]
2810

2911
int order = LOWEST_PRECEDENCE
3012

@@ -42,8 +24,6 @@ class LoginInterceptor {
4224
return true
4325
}
4426

45-
def isAdmin = spatialAuthService.userInRole(spatialConfig.auth.admin_role)
46-
4727
def controller = grailsApplication.getArtefactByLogicalPropertyName("Controller", controllerName)
4828
Class controllerClass = controller?.clazz
4929
def method = controllerClass?.getMethod(actionName ?: "index", [] as Class[])
@@ -55,18 +35,14 @@ class LoginInterceptor {
5535
//Calculating the required permission.
5636
def permissionLevel = null
5737
//Permission on method has the top priority
58-
if (method?.isAnnotationPresent(RequirePermission.class)) {
59-
permissionLevel = RequirePermission
60-
} else if (method?.isAnnotationPresent(RequireLogin.class)) {
38+
if (method?.isAnnotationPresent(RequireLogin.class)) {
6139
permissionLevel = RequireLogin
6240
} else if (method?.isAnnotationPresent(RequireAdmin.class)) {
6341
permissionLevel = RequireAdmin
6442
}
6543

6644
if (Objects.isNull(permissionLevel)) {
67-
if (controllerClass?.isAnnotationPresent(RequirePermission.class)) {
68-
permissionLevel = RequirePermission
69-
} else if (controllerClass?.isAnnotationPresent(RequireLogin.class)) {
45+
if (controllerClass?.isAnnotationPresent(RequireLogin.class)) {
7046
permissionLevel = RequireLogin
7147
} else if (controllerClass?.isAnnotationPresent(RequireAdmin.class)) {
7248
permissionLevel = RequireAdmin
@@ -75,15 +51,7 @@ class LoginInterceptor {
7551

7652
//Permission check
7753
def role // if require a certain level of ROLE
78-
if (hasValidApiKey()) {
79-
return true
80-
} else if (permissionLevel == RequirePermission) {
81-
if (authService.getUserId()) {
82-
return true
83-
} else {
84-
return accessDenied(STATUS_UNAUTHORISED, 'Forbidden, ApiKey or user login required!')
85-
}
86-
} else if (permissionLevel == RequireAdmin) {
54+
if (permissionLevel == RequireAdmin) {
8755
role = spatialConfig.auth.admin_role
8856
} else if (permissionLevel == RequireLogin) {
8957
RequireLogin requireAuthentication = method.getAnnotation(RequireLogin.class)
@@ -126,44 +94,4 @@ class LoginInterceptor {
12694
return false
12795
}
12896
}
129-
130-
def hasValidApiKey() {
131-
isValid(getApiKey())
132-
}
133-
134-
def isValid(key) {
135-
if (key == null) {
136-
return false
137-
}
138-
139-
Boolean result = testedKeys.get(key)
140-
141-
if (result == null) {
142-
String url = MessageFormat.format(grailsApplication.config.apiKeyCheckUrlTemplate.toString(), key)
143-
144-
result = key == grailsApplication.config.serviceKey || Util.getUrl(url).contains('"valid":true')
145-
testedKeys.put(key, result)
146-
}
147-
148-
return result
149-
}
150-
151-
private getApiKey() {
152-
String apikey
153-
def request = WebUtils.retrieveGrailsWebRequest().getCurrentRequest()
154-
155-
for (name in API_KEY_HEADER_NAME) {
156-
if (request.getHeader(name)) {
157-
return request.getHeader(name).split(",|;")[0]
158-
}
159-
if (request.getParameter(name)) {
160-
return request.getParameter(name).split(",|;")[0]
161-
}
162-
}
163-
164-
//Last try
165-
apikey = request.JSON?.api_key
166-
167-
apikey
168-
}
16997
}

grails-app/controllers/au/org/ala/spatial/ManageLayersController.groovy

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import javax.servlet.http.HttpServletResponse
2727
import javax.transaction.Transactional
2828
import java.text.SimpleDateFormat
2929

30-
@RequirePermission
3130
@Transactional
3231
class ManageLayersController {
3332

@@ -41,10 +40,11 @@ class ManageLayersController {
4140
AuthService authService
4241

4342
/**
44-
* admin only or api_key
43+
* admin only
4544
*
4645
* @return
4746
*/
47+
@RequireAdmin
4848
def index() {
4949
}
5050

@@ -53,6 +53,7 @@ class ManageLayersController {
5353
*
5454
* @return
5555
*/
56+
@RequireAdmin
5657
def layers() {
5758
log.debug("List avaliable layers")
5859
Map map = [:]
@@ -121,7 +122,6 @@ class ManageLayersController {
121122
*
122123
* @return
123124
*/
124-
125125
@RequireAdmin
126126
def uploads() {
127127
Map map = [:]
@@ -174,7 +174,6 @@ class ManageLayersController {
174174
* admin only
175175
*
176176
* @param req
177-
* @param apiKey
178177
* @return
179178
* @throws Exception
180179
*/

grails-app/controllers/au/org/ala/spatial/ShapesController.groovy

-3
Original file line numberDiff line numberDiff line change
@@ -647,12 +647,9 @@ class ShapesController {
647647
JSONRequestBodyParser reqBodyParser = new JSONRequestBodyParser()
648648
reqBodyParser.addParameter("user_id", String.class, false)
649649
reqBodyParser.addParameter("shp_file_url", String.class, false)
650-
reqBodyParser.addParameter("api_key", String.class, false)
651650

652651
if (reqBodyParser.parseJSON(jsonRequestBody)) {
653-
654652
String shpFileUrl = (String) reqBodyParser.getParsedValue("shp_file_url")
655-
String apiKey = (String) reqBodyParser.getParsedValue("api_key")
656653

657654
// Use shape file url from json body
658655
FileUtils.copyURLToFile(new URL(shpFileUrl), tmpZipFile)

0 commit comments

Comments
 (0)