Skip to content
Permalink
Browse files

fix(sipi): Don't expect API v1 status code (DSP-1114) (#1763)

  • Loading branch information
benjamingeer committed Dec 4, 2020
1 parent a02ac50 commit 3236d2571b98db26444aa3b448a42a88345e9075
@@ -19,44 +19,60 @@

package org.knora.salsah

import java.net.{NetworkInterface, UnknownHostException}

import com.typesafe.config.{Config, ConfigFactory}
import org.testcontainers.containers.{BindMode, GenericContainer}
import org.testcontainers.utility.DockerImageName

import scala.collection.JavaConverters._

/**
* Provides all containers necessary for running tests.
*/
object TestContainers {

val FusekiContainer = new GenericContainer("daschswiss/knora-jena-fuseki:latest")
// get local IP address, which we need for SIPI
val localIpAddress: String = NetworkInterface.getNetworkInterfaces
.asScala.toSeq.filter(!_.isLoopback)
.flatMap(_.getInetAddresses.asScala.toSeq.filter(_.getAddress.length == 4).map(_.toString))
.headOption.getOrElse(throw new UnknownHostException("No suitable network interface found"))

val FusekiImageName: DockerImageName = DockerImageName.parse("bazel/docker/knora-jena-fuseki:image")
val FusekiContainer = new GenericContainer(FusekiImageName)
FusekiContainer.withExposedPorts(3030)
FusekiContainer.withEnv("ADMIN_PASSWORD", "test")
FusekiContainer.withEnv("JVM_ARGS", "-Xmx3G")
FusekiContainer.start()

val SipiContainer = new GenericContainer("daschswiss/knora-sipi:latest")
val SipiImageName: DockerImageName = DockerImageName.parse("bazel/docker/knora-sipi:image")
val SipiContainer = new GenericContainer(SipiImageName)
SipiContainer.withExposedPorts(1024)
SipiContainer.withEnv("SIPI_EXTERNAL_PROTOCOL", "http")
SipiContainer.withEnv("SIPI_EXTERNAL_HOSTNAME", "sipi")
SipiContainer.withEnv("SIPI_EXTERNAL_PORT", "1024")
SipiContainer.withEnv("SIPI_WEBAPI_HOSTNAME", "api")
SipiContainer.withEnv("SIPI_WEBAPI_HOSTNAME", localIpAddress)
SipiContainer.withEnv("SIPI_WEBAPI_PORT", "3333")
SipiContainer.withCommand("--config=/sipi/config/sipi.knora-docker-config.lua")
SipiContainer.withClasspathResourceMapping("/sipi/config/sipi.knora-docker-config.lua",
SipiContainer.withClasspathResourceMapping(
"/sipi/config/sipi.knora-docker-config.lua",
"/sipi/config/sipi.knora-docker-config.lua",
BindMode.READ_ONLY)
SipiContainer.withClasspathResourceMapping("/sipi/config/sipi.knora-docker-config.lua",
"/sipi/config/sipi.init-knora.lua",
BindMode.READ_ONLY)
SipiContainer.start()

val RedisContainer = new GenericContainer("redis:5")
// Container needs to be started to get the random IP
val sipiIp: String = SipiContainer.getHost
val sipiPort: Int = SipiContainer.getFirstMappedPort

val RedisImageName: DockerImageName = DockerImageName.parse("redis:5")
val RedisContainer = new GenericContainer(RedisImageName)
RedisContainer.withExposedPorts(6379)
RedisContainer.start()

import scala.collection.JavaConverters._
private val portMap = Map(
"app.triplestore.fuseki.port" -> FusekiContainer.getFirstMappedPort,
"app.sipi.internal-port" -> SipiContainer.getFirstMappedPort,
"app.sipi.external-host" -> sipiIp,
"app.sipi.external-port" -> sipiPort,
"app.sipi.internal-host" -> sipiIp,
"app.sipi.internal-port" -> sipiPort,
"app.cache-service.redis.port" -> RedisContainer.getFirstMappedPort
).asJava

@@ -3,9 +3,6 @@ package(default_visibility = ["//visibility:public"])
filegroup(
name = "config",
srcs = [
"sipi.init-knora.lua",
"sipi.init-knora-no-auth.lua",
"sipi.init-knora-test.lua",
"sipi.knora-docker-config.lua",
"sipi.knora-docker-no-auth-config.lua",
"sipi.knora-docker-test-config.lua",
@@ -82,7 +82,7 @@ sipi = {
--
-- Lua script which is executed on initialization of the Lua interpreter
--
initscript = '/sipi/config/sipi.init-knora.lua',
initscript = '/sipi/scripts/sipi.init-knora.lua',

--
-- path to the caching directory
@@ -63,7 +63,7 @@ sipi = {
--
-- Lua script which is executed on initialization of the Lua interpreter
--
initscript = '/sipi/config/sipi.init-knora-no-auth.lua',
initscript = '/sipi/scripts/sipi.init-knora-no-auth.lua',

--
-- path to the caching directory
@@ -82,7 +82,7 @@ sipi = {
--
-- Lua script which is executed on initialization of the Lua interpreter
--
initscript = './config/sipi.init-knora-test.lua',
initscript = './scripts/sipi.init-knora-test.lua',

--
-- path to the caching directory
@@ -82,7 +82,7 @@ sipi = {
--
-- Lua script which is executed on initialization of the Lua interpreter
--
initscript = './config/sipi.init-knora.lua',
initscript = './scripts/sipi.init-knora.lua',

--
-- path to the caching directory
@@ -34,6 +34,7 @@ require "get_knora_session"
-- filepath: server-path where the master file is located
-------------------------------------------------------------------------------
function pre_flight(prefix,identifier,cookie)
server.log("pre_flight called in sipi.init-knora-no-auth.lua", server.loglevel.LOG_DEBUG)

--
-- Allways allows access to images. No authorization from Knora is retrieved
@@ -34,6 +34,8 @@ require "get_knora_session"
-- filepath: server-path where the master file is located
-------------------------------------------------------------------------------
function pre_flight(prefix,identifier,cookie)
server.log("pre_flight called in sipi.init-knora-test.lua", server.loglevel.LOG_DEBUG)


--
-- For Knora Sipi integration testing
@@ -98,14 +100,8 @@ function pre_flight(prefix,identifier,cookie)
return 'deny'
end

server.log("pre_flight - status: " .. response_json.status, server.loglevel.LOG_DEBUG)
server.log("pre_flight - permission code: " .. response_json.permissionCode, server.loglevel.LOG_DEBUG)

if response_json.status ~= 0 then
-- something went wrong with the request, Knora returned a non zero status
return 'deny'
end

if response_json.permissionCode == 0 then
-- no view permission on file
return 'deny'
@@ -114,11 +110,20 @@ function pre_flight(prefix,identifier,cookie)
-- either watermark or size (depends on project, should be returned with permission code by Sipi responder)
-- currently, only size is used

server.log("pre_flight - restricted view settings - size: " .. tostring(response_json.restrictedViewSettings.size), server.loglevel.LOG_DEBUG)
server.log("pre_flight - restricted view settings - watermark: " .. tostring(response_json.restrictedViewSettings.watermark), server.loglevel.LOG_DEBUG)
local restrictedViewSize

local restrictedViewSize = response_json.restrictedViewSettings.size
if restrictedViewSize == nil then
if response_json.restrictedViewSettings ~= nil then
-- server.log("pre_flight - restricted view settings - watermark: " .. tostring(response_json.restrictedViewSettings.watermark), server.loglevel.LOG_DEBUG)

if response_json.restrictedViewSettings.size ~= nil then
server.log("pre_flight - restricted view settings - size: " .. tostring(response_json.restrictedViewSettings.size), server.loglevel.LOG_DEBUG)
restrictedViewSize = response_json.restrictedViewSettings.size
else
server.log("pre_flight - using default restricted view size", server.loglevel.LOG_DEBUG)
restrictedViewSize = config.thumb_size
end
else
server.log("pre_flight - using default restricted view size", server.loglevel.LOG_DEBUG)
restrictedViewSize = config.thumb_size
end

@@ -34,6 +34,7 @@ require "get_knora_session"
-- filepath: server-path where the master file is located
-------------------------------------------------------------------------------
function pre_flight(prefix, identifier, cookie)
server.log("pre_flight called in sipi.init-knora.lua", server.loglevel.LOG_DEBUG)

if config.prefix_as_path then
filepath = config.imgroot .. '/' .. prefix .. '/' .. identifier
@@ -115,14 +116,8 @@ function pre_flight(prefix, identifier, cookie)
return 'deny'
end

server.log("pre_flight - status: " .. response_json.status, server.loglevel.LOG_DEBUG)
server.log("pre_flight - permission code: " .. response_json.permissionCode, server.loglevel.LOG_DEBUG)

if response_json.status ~= 0 then
-- something went wrong with the request, Knora returned a non zero status
return 'deny'
end

if response_json.permissionCode == 0 then
-- no view permission on file
return 'deny'
@@ -131,11 +126,20 @@ function pre_flight(prefix, identifier, cookie)
-- either watermark or size (depends on project, should be returned with permission code by Sipi responder)
-- currently, only size is used

server.log("pre_flight - restricted view settings - size: " .. tostring(response_json.restrictedViewSettings.size), server.loglevel.LOG_DEBUG)
server.log("pre_flight - restricted view settings - watermark: " .. tostring(response_json.restrictedViewSettings.watermark), server.loglevel.LOG_DEBUG)
local restrictedViewSize

local restrictedViewSize = response_json.restrictedViewSettings.size
if restrictedViewSize == nil then
if response_json.restrictedViewSettings ~= nil then
-- server.log("pre_flight - restricted view settings - watermark: " .. tostring(response_json.restrictedViewSettings.watermark), server.loglevel.LOG_DEBUG)

if response_json.restrictedViewSettings.size ~= nil then
server.log("pre_flight - restricted view settings - size: " .. tostring(response_json.restrictedViewSettings.size), server.loglevel.LOG_DEBUG)
restrictedViewSize = response_json.restrictedViewSettings.size
else
server.log("pre_flight - using default restricted view size", server.loglevel.LOG_DEBUG)
restrictedViewSize = config.thumb_size
end
else
server.log("pre_flight - using default restricted view size", server.loglevel.LOG_DEBUG)
restrictedViewSize = config.thumb_size
end

@@ -19,38 +19,50 @@

package org.knora.webapi

import java.net.{NetworkInterface, UnknownHostException}

import com.typesafe.config.{Config, ConfigFactory}
import org.testcontainers.containers.{BindMode, GenericContainer}
import org.testcontainers.utility.DockerImageName

import scala.collection.JavaConverters._

/**
* Provides all containers necessary for running tests.
*/
object TestContainers {

val FusekiContainer = new GenericContainer("bazel/docker/knora-jena-fuseki:image")
// get local IP address, which we need for SIPI
val localIpAddress: String = NetworkInterface.getNetworkInterfaces
.asScala.toSeq.filter(!_.isLoopback)
.flatMap(_.getInetAddresses.asScala.toSeq.filter(_.getAddress.length == 4).map(_.toString))
.headOption.getOrElse(throw new UnknownHostException("No suitable network interface found"))

val FusekiImageName: DockerImageName = DockerImageName.parse("bazel/docker/knora-jena-fuseki:image")
val FusekiContainer = new GenericContainer(FusekiImageName)
FusekiContainer.withExposedPorts(3030)
FusekiContainer.withEnv("ADMIN_PASSWORD", "test")
FusekiContainer.withEnv("JVM_ARGS", "-Xmx3G")
FusekiContainer.start()

val SipiContainer = new GenericContainer("bazel/docker/knora-sipi:image")
val SipiImageName: DockerImageName = DockerImageName.parse("bazel/docker/knora-sipi:image")
val SipiContainer = new GenericContainer(SipiImageName)
SipiContainer.withExposedPorts(1024)
SipiContainer.withEnv("SIPI_WEBAPI_HOSTNAME", "api")
SipiContainer.withEnv("SIPI_WEBAPI_HOSTNAME", localIpAddress)
SipiContainer.withEnv("SIPI_WEBAPI_PORT", "3333")
SipiContainer.withCommand("--config=/sipi/config/sipi.knora-docker-config.lua")
SipiContainer.withClasspathResourceMapping("/sipi/config/sipi.knora-docker-config.lua",
SipiContainer.withClasspathResourceMapping(
"/sipi/config/sipi.knora-docker-config.lua",
"/sipi/config/sipi.knora-docker-config.lua",
BindMode.READ_ONLY)
SipiContainer.withClasspathResourceMapping("/sipi/config/sipi.knora-docker-config.lua",
"/sipi/config/sipi.init-knora.lua",
BindMode.READ_ONLY)
SipiContainer.start()

// Container needs to be started to get the random IP
val sipiIp: IRI = SipiContainer.getHost
val sipiIp: String = SipiContainer.getHost
val sipiPort: Int = SipiContainer.getFirstMappedPort

val RedisContainer = new GenericContainer("redis:5")
val RedisImageName: DockerImageName = DockerImageName.parse("redis:5")
val RedisContainer = new GenericContainer(RedisImageName)
RedisContainer.withExposedPorts(6379)
RedisContainer.start()

@@ -54,7 +54,7 @@ object KnoraSipiIntegrationV2ITSpec {
class KnoraSipiIntegrationV2ITSpec extends ITKnoraLiveSpec(KnoraSipiIntegrationV2ITSpec.config) with AuthenticationV2JsonProtocol with TriplestoreJsonProtocol {
private implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance

private val anythingUserEmail = SharedTestDataADM.anythingUser1.email
private val anythingUserEmail = SharedTestDataADM.anythingAdminUser.email
private val incunabulaUserEmail = SharedTestDataADM.incunabulaMemberUser.email
private val password = SharedTestDataADM.testPass

@@ -397,7 +397,8 @@ class KnoraSipiIntegrationV2ITSpec extends ITKnoraLiveSpec(KnoraSipiIntegrationV
| "knora-api:hasStillImageFileValue" : {
| "@id" : "${stillImageFileValueIri.get}",
| "@type" : "knora-api:StillImageFileValue",
| "knora-api:fileValueHasFilename" : "${uploadedFile.internalFilename}"
| "knora-api:fileValueHasFilename" : "${uploadedFile.internalFilename}",
| "knora-api:hasPermissions" : "CR knora-admin:Creator|V knora-admin:UnknownUser"
| },
| "@context" : {
| "knora-api" : "http://api.knora.org/ontology/knora-api/v2#",
@@ -430,7 +431,7 @@ class KnoraSipiIntegrationV2ITSpec extends ITKnoraLiveSpec(KnoraSipiIntegrationV
val sipiGetImageRequest = Get(savedImage.iiifUrl)
checkResponseOK(sipiGetImageRequest)
}

/*
"delete the temporary file if Knora rejects the request to create a file value" in {
// Upload the image to Sipi.
val sipiUploadResponse: SipiUploadResponse = uploadToSipi(
@@ -846,6 +847,6 @@ class KnoraSipiIntegrationV2ITSpec extends ITKnoraLiveSpec(KnoraSipiIntegrationV
val savedTextFile: SavedTextFile = savedValueToSavedTextFile(savedValue)
assert(savedTextFile.internalFilename == uploadedFile.internalFilename)
}
}*/
}
}
@@ -45,7 +45,7 @@ case class SipiFileInfoGetRequestADM(projectID: String,
requestingUser: UserADM) extends SipiResponderRequestADM

/**
* Represents the Knora API v1 JSON response to a request for a information about a `FileValue`.
* Represents the JSON response to a request for a information about a `FileValue`.
*
* @param permissionCode a code representing the user's maximum permission on the file.
* @param restrictedViewSettings the project's restricted view settings.
@@ -112,6 +112,8 @@ class SipiResponderADM(responderData: ResponderData) extends Responder(responder

case _ => FastFuture.successful(SipiFileInfoGetResponseADM(permissionCode = permissionCode, restrictedViewSettings = None))
}

_ = println(s"SipiResponderADM returning permission code $permissionCode")
} yield response
}
}
@@ -49,6 +49,7 @@ class SipiRouteADM(routeData: KnoraRouteData) extends KnoraRoute(routeData) with
)
projectID = stringFormatter.validateProjectShortcode(projectIDAndFile.head, throw BadRequestException(s"Invalid project ID: '${projectIDAndFile.head}'"))
filename = stringFormatter.toSparqlEncodedString(projectIDAndFile(1), throw BadRequestException(s"Invalid filename: '${projectIDAndFile(1)}'"))
_ = println(s"/admin/files route called for filename $filename")
} yield SipiFileInfoGetRequestADM(
projectID = projectID,
filename = filename,

0 comments on commit 3236d25

Please sign in to comment.