-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated validation documents to prevent old packets from going into d…
…atabase. More integration tests
- Loading branch information
1 parent
301c413
commit ccf7500
Showing
10 changed files
with
276 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...nt/src/test/java/me/retrodaredevil/solarthing/integration/DatabaseOpenUploadOnlyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package me.retrodaredevil.solarthing.integration; | ||
|
||
import me.retrodaredevil.couchdbjava.CouchDbInstance; | ||
import me.retrodaredevil.couchdbjava.exception.CouchDbException; | ||
import me.retrodaredevil.couchdbjava.exception.CouchDbUnauthorizedException; | ||
import me.retrodaredevil.solarthing.SolarThingDatabaseType; | ||
import me.retrodaredevil.solarthing.database.SolarThingDatabase; | ||
import me.retrodaredevil.solarthing.database.UpdateToken; | ||
import me.retrodaredevil.solarthing.database.couchdb.CouchDbSolarThingDatabase; | ||
import me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException; | ||
import me.retrodaredevil.solarthing.misc.device.CelsiusCpuTemperaturePacket; | ||
import me.retrodaredevil.solarthing.packets.collection.PacketCollection; | ||
import me.retrodaredevil.solarthing.packets.collection.PacketCollectionIdGenerator; | ||
import me.retrodaredevil.solarthing.packets.collection.PacketCollections; | ||
import me.retrodaredevil.solarthing.packets.instance.InstanceFragmentIndicatorPackets; | ||
import me.retrodaredevil.solarthing.packets.instance.InstanceSourcePackets; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.util.Arrays; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
|
||
@Tag("integration") | ||
public class DatabaseOpenUploadOnlyTest { | ||
|
||
private static PacketCollection createSimplePacketCollection() { | ||
// Note that in a production environment, we would make sure that the packets in a given packet collection | ||
// actually belong in a particular database, but for testing we don't care. (A CpuTemperaturePacket shouldn't end up in the events database) | ||
return PacketCollections.createFromPackets( | ||
Instant.now(), | ||
Arrays.asList( | ||
new CelsiusCpuTemperaturePacket(null, 20.2f, null), | ||
InstanceSourcePackets.create("default"), | ||
InstanceFragmentIndicatorPackets.create(1) | ||
), | ||
PacketCollectionIdGenerator.Defaults.UNIQUE_GENERATOR, | ||
ZoneId.systemDefault() | ||
); | ||
} | ||
|
||
|
||
@ParameterizedTest | ||
@MethodSource("me.retrodaredevil.solarthing.integration.DatabaseService#all") | ||
void test(DatabaseService databaseService) throws CouchDbException, SolarThingDatabaseException { | ||
IntegrationSetup.setup(IntegrationUtil.createCouchDbInstance(databaseService, IntegrationUtil.DEFAULT_ADMIN_AUTH)); | ||
|
||
// Uploader user | ||
CouchDbInstance uploaderInstance = IntegrationUtil.createCouchDbInstance(databaseService, IntegrationUtil.getAuthFor(SolarThingDatabaseType.UserType.UPLOADER)); | ||
SolarThingDatabase database = CouchDbSolarThingDatabase.create(uploaderInstance); | ||
|
||
PacketCollection packetCollection = createSimplePacketCollection(); | ||
UpdateToken updateToken = database.getOpenDatabase().uploadPacketCollection(packetCollection, null); | ||
try { | ||
database.getOpenDatabase().uploadPacketCollection(packetCollection, updateToken); | ||
} catch (SolarThingDatabaseException solarThingDatabaseException) { | ||
// expect that we are unauthorized to change the document | ||
assertInstanceOf(CouchDbUnauthorizedException.class, solarThingDatabaseException.getCause()); | ||
} | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
.../src/test/java/me/retrodaredevil/solarthing/integration/DatabaseRejectOldPacketsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package me.retrodaredevil.solarthing.integration; | ||
|
||
import me.retrodaredevil.couchdbjava.CouchDbInstance; | ||
import me.retrodaredevil.couchdbjava.exception.CouchDbCodeException; | ||
import me.retrodaredevil.couchdbjava.exception.CouchDbException; | ||
import me.retrodaredevil.couchdbjava.response.ErrorResponse; | ||
import me.retrodaredevil.solarthing.SolarThingDatabaseType; | ||
import me.retrodaredevil.solarthing.database.SolarThingDatabase; | ||
import me.retrodaredevil.solarthing.database.couchdb.CouchDbSolarThingDatabase; | ||
import me.retrodaredevil.solarthing.database.exception.SolarThingDatabaseException; | ||
import me.retrodaredevil.solarthing.misc.device.CelsiusCpuTemperaturePacket; | ||
import me.retrodaredevil.solarthing.packets.collection.PacketCollection; | ||
import me.retrodaredevil.solarthing.packets.collection.PacketCollectionIdGenerator; | ||
import me.retrodaredevil.solarthing.packets.collection.PacketCollections; | ||
import me.retrodaredevil.solarthing.packets.instance.InstanceFragmentIndicatorPackets; | ||
import me.retrodaredevil.solarthing.packets.instance.InstanceSourcePackets; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.time.ZoneId; | ||
import java.util.Arrays; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
@Tag("integration") | ||
public class DatabaseRejectOldPacketsTest { | ||
|
||
private static PacketCollection createSimplePacketCollection(Instant instant) { | ||
// Note that in a production environment, we would make sure that the packets in a given packet collection | ||
// actually belong in a particular database, but for testing we don't care. (A CpuTemperaturePacket shouldn't end up in the events database) | ||
return PacketCollections.createFromPackets( | ||
instant, | ||
Arrays.asList( | ||
new CelsiusCpuTemperaturePacket(null, 20.2f, null), | ||
InstanceSourcePackets.create("default"), | ||
InstanceFragmentIndicatorPackets.create(1) | ||
), | ||
PacketCollectionIdGenerator.Defaults.UNIQUE_GENERATOR, | ||
ZoneId.systemDefault() | ||
); | ||
} | ||
|
||
|
||
@ParameterizedTest | ||
@MethodSource("me.retrodaredevil.solarthing.integration.DatabaseService#all") | ||
void test(DatabaseService databaseService) throws CouchDbException, SolarThingDatabaseException { | ||
IntegrationSetup.setup(IntegrationUtil.createCouchDbInstance(databaseService, IntegrationUtil.DEFAULT_ADMIN_AUTH)); | ||
|
||
// Uploader user | ||
CouchDbInstance uploaderInstance = IntegrationUtil.createCouchDbInstance(databaseService, IntegrationUtil.getAuthFor(SolarThingDatabaseType.UserType.UPLOADER)); | ||
SolarThingDatabase database = CouchDbSolarThingDatabase.create(uploaderInstance); | ||
|
||
Instant pastInstant = Instant.now().minus(Duration.ofMinutes(30)); | ||
Instant futureInstant = Instant.now().plus(Duration.ofMinutes(4)); | ||
Instant tinyBitInTheFutureInstant = Instant.now().plus(Duration.ofMinutes(1)); | ||
PacketCollection pastPacketCollection = createSimplePacketCollection(pastInstant); | ||
try { | ||
database.getStatusDatabase().uploadPacketCollection(pastPacketCollection, null); | ||
fail("We expect this to fail"); | ||
} catch (SolarThingDatabaseException solarThingDatabaseException) { | ||
expectForbiddenResponse(solarThingDatabaseException); | ||
} | ||
try { | ||
database.getEventDatabase().uploadPacketCollection(pastPacketCollection, null); | ||
fail("We expect this to fail"); | ||
} catch (SolarThingDatabaseException solarThingDatabaseException) { | ||
expectForbiddenResponse(solarThingDatabaseException); | ||
} | ||
|
||
try { | ||
database.getOpenDatabase().uploadPacketCollection(pastPacketCollection, null); | ||
fail("We expect this to fail"); | ||
} catch (SolarThingDatabaseException solarThingDatabaseException) { | ||
expectForbiddenResponse(solarThingDatabaseException); | ||
} | ||
try { | ||
// future solarthing_open test | ||
database.getOpenDatabase().uploadPacketCollection(createSimplePacketCollection(futureInstant), null); | ||
fail("We expect this to fail"); | ||
} catch (SolarThingDatabaseException solarThingDatabaseException) { | ||
expectForbiddenResponse(solarThingDatabaseException); | ||
} | ||
// We expect to be able to upload a tiny bit in the future to SolarThing open | ||
database.getOpenDatabase().uploadPacketCollection(createSimplePacketCollection(tinyBitInTheFutureInstant), null); | ||
} | ||
private static void expectForbiddenResponse(SolarThingDatabaseException solarThingDatabaseException) { | ||
// NOTE: The cause is an implementation detail of a SolarThingDatabaseException | ||
// Remember that we don't want to rely on implementation details in actual code, | ||
// but it's OK in a test like this. | ||
assertInstanceOf(CouchDbCodeException.class, solarThingDatabaseException.getCause()); | ||
|
||
CouchDbCodeException e = (CouchDbCodeException) solarThingDatabaseException.getCause(); | ||
|
||
// We expect to have thrown a forbidden error as described here: https://docs.couchdb.org/en/stable/ddocs/ddocs.html#validate-document-update-functions | ||
assertEquals(403, e.getCode()); | ||
ErrorResponse errorResponse = e.getErrorResponse(); | ||
assertNotNull(errorResponse); | ||
assertEquals("forbidden", errorResponse.getError()); | ||
assertTrue(errorResponse.getReason().contains("dateMillis")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 38 additions & 14 deletions
52
core/src/main/resources/me/retrodaredevil/couchdb/design/validation/readonly_auth.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,49 @@ | ||
// solarthing, solarthing_alter, solarthing_events, solarthing_closed | ||
|
||
// noinspection JSUnusedGlobalSymbols,ReservedWordAsName | ||
|
||
// Readonly auth is designed to allow databases to be made public to the world, but not allow edits. | ||
// This also makes sure that any document with a dateMillis property is not uploading old data. | ||
// This will not affect documents without a dateMillis property, so this can be used for many databases. | ||
|
||
function(newDoc, oldDoc, userCtx, secObj) { | ||
var is_server_or_database_admin = function(userCtx, secObj) { | ||
// see if the user is a server admin | ||
if(userCtx.roles.indexOf('_admin') !== -1) { | ||
return true; // a server admin | ||
} | ||
|
||
secObj.admins = secObj.admins || {}; | ||
secObj.admins.names = secObj.admins.names || []; | ||
secObj.admins.roles = secObj.admins.roles || []; | ||
// see if the user a database admin specified by name | ||
if(secObj && secObj.admins && secObj.admins.names) { | ||
if(secObj.admins.names.indexOf(userCtx.name) !== -1) { | ||
return true; // database admin | ||
} | ||
} | ||
|
||
var isAdmin = false; | ||
if(userCtx.roles.indexOf('_admin') !== -1) { | ||
isAdmin = true; | ||
} | ||
if(secObj.admins.names.indexOf(userCtx.name) !== -1) { | ||
isAdmin = true; | ||
} | ||
for(var i = 0; i < userCtx.roles.length; i++) { | ||
if(secObj.admins.roles.indexOf(userCtx.roles[i]) !== -1) { | ||
isAdmin = true; | ||
// see if the user a database admin specified by role | ||
if(secObj && secObj.admins && secObj.admins.roles) { | ||
var db_roles = secObj.admins.roles; | ||
for(var idx = 0; idx < userCtx.roles.length; idx++) { | ||
var user_role = userCtx.roles[idx]; | ||
if(db_roles.indexOf(user_role) !== -1) { | ||
return true; // role matches! | ||
} | ||
} | ||
} | ||
|
||
return false; // default to no admin | ||
} | ||
|
||
if(!isAdmin) { | ||
if(!is_server_or_database_admin(userCtx, secObj)) { | ||
throw {'unauthorized':'This is read only when unauthorized'}; | ||
} | ||
|
||
// Only check dateMillis if this is a new document and dateMillis is present on the packet | ||
if (!oldDoc && newDoc.dateMillis !== undefined) { | ||
var currentMillis = +new Date(); | ||
var millisInPast = currentMillis - newDoc.dateMillis; | ||
if (millisInPast > 25 * 60 * 1000) { | ||
throw {forbidden: "dateMillis field is too far in the past!"} | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
.../main/resources/me/retrodaredevil/couchdb/design/validation/solarthing_open_validation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// This validation function made specifically for solarthing_open | ||
|
||
function(newDoc, oldDoc, userCtx, secObj) { | ||
var is_server_or_database_admin = function(userCtx, secObj) { | ||
// see if the user is a server admin | ||
if(userCtx.roles.indexOf('_admin') !== -1) { | ||
return true; // a server admin | ||
} | ||
|
||
// see if the user a database admin specified by name | ||
if(secObj && secObj.admins && secObj.admins.names) { | ||
if(secObj.admins.names.indexOf(userCtx.name) !== -1) { | ||
return true; // database admin | ||
} | ||
} | ||
|
||
// see if the user a database admin specified by role | ||
if(secObj && secObj.admins && secObj.admins.roles) { | ||
var db_roles = secObj.admins.roles; | ||
for(var idx = 0; idx < userCtx.roles.length; idx++) { | ||
var user_role = userCtx.roles[idx]; | ||
if(db_roles.indexOf(user_role) !== -1) { | ||
return true; // role matches! | ||
} | ||
} | ||
} | ||
|
||
return false; // default to no admin | ||
} | ||
|
||
// Perform certain checks only if this is a non-admin user | ||
if (!is_server_or_database_admin(userCtx, secObj)) { | ||
if (oldDoc) { // If someone is trying to update a document, prevent them from doing that | ||
throw {'unauthorized':'You are not authorized to change this document!'}; | ||
} | ||
if (typeof newDoc.dateMillis !== "number") { | ||
throw {forbidden: "dateMillis must be a number"} | ||
} | ||
var currentMillis = +new Date(); | ||
var millisInPast = currentMillis - newDoc.dateMillis; | ||
// Not only do we not want people to upload data that's far in the past, | ||
// we have strict rules that disallow documents targeted for the future. | ||
if (millisInPast > 25 * 60 * 1000) { | ||
throw {forbidden: "dateMillis field is too far in the past!"} | ||
} | ||
if (-millisInPast > 3 * 60 * 1000) { | ||
throw {forbidden: "dateMillis field is too far in the future!"} | ||
} | ||
} | ||
} |
26 changes: 0 additions & 26 deletions
26
core/src/main/resources/me/retrodaredevil/couchdb/design/validation/upload_only.js
This file was deleted.
Oops, something went wrong.