-
Notifications
You must be signed in to change notification settings - Fork 12
/
iiVault.r
233 lines (212 loc) · 9.1 KB
/
iiVault.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# \file iiVault.r
# \brief Functions to copy packages to the vault and manage permissions of vault packages.
# \author Paul Frederiks
# \author Lazlo Westerhof
# \copyright Copyright (c) 2016-2022, Utrecht University. All rights reserved.
# \license GPLv3, see LICENSE.
# \brief Called by uuTreeWalk for each collection and dataobject to copy to the vault.
#
# \param[in] itemParent
# \param[in] itemName
# \param[in] itemIsCollection
# \param[in/out] buffer
# \param[in/out] error
#
iiIngestObject(*itemParent, *itemName, *itemIsCollection, *buffer, *error) {
*sourcePath = "*itemParent/*itemName";
msiCheckAccess(*sourcePath, "read object", *readAccess);
if (*readAccess != 1) {
*error = errorcode(msiSetACL("default", "admin:read", uuClientFullName, *sourcePath));
if (*error < 0) {
*buffer.msg = "Failed to acquire read access to *sourcePath";
succeed;
} else {
writeLine("stdout", "iiIngestObject: Read access to *sourcePath acquired");
}
}
*destPath = *buffer.destination;
if (*sourcePath != *buffer."source") {
# rewrite path to copy objects that are located underneath the toplevel collection
*sourceLength = strlen(*sourcePath);
*relativePath = substr(*sourcePath, strlen(*buffer."source") + 1, *sourceLength);
*destPath = *buffer."destination" ++ "/" ++ *relativePath;
*markIncomplete = false;
} else {
*markIncomplete = true;
}
if (*itemIsCollection) {
*error = errorcode(msiCollCreate(*destPath, 1, *status));
if (*error < 0) {
*buffer.msg = "Failed to create collection *destPath";
} else if (*markIncomplete) {
# The root collection of the vault package is marked incomplete until the last step in FolderSecure
*vaultStatus = IIVAULTSTATUSATTRNAME;
msiString2KeyValPair("*vaultStatus=" ++ INCOMPLETE, *kvp);
msiAssociateKeyValuePairsToObj(*kvp, *destPath, "-C");
}
} else {
# Copy data object to vault and compute checksum.
*resource = "";
*numThreads = "";
*err1 = errorcode(rule_resource_vault(*resource));
*err2 = errorcode(rule_vault_copy_numthreads(*numThreads));
*error = errorcode(msiDataObjCopy(*sourcePath, *destPath, "destRescName=" ++ *resource ++ "++++numThreads=" ++ *numThreads ++ "++++verifyChksum=", *status));
if (*error < 0) {
*buffer.msg = "Failed to copy *sourcePath to *destPath";
}
}
if (*readAccess != 1) {
*error = errorcode(msiSetACL("default", "admin:null", uuClientFullName, *sourcePath));
if (*error < 0) {
*buffer.msg = "Failed to revoke read access to *sourcePath";
} else {
writeLine("stdout", "iiIngestObject: Read access to *sourcePath revoked");
}
}
}
# \brief Called by uuTreeWalk for each collection and dataobject to copy to the research area.
#
# \param[in] itemParent
# \param[in] itemName
# \param[in] itemIsCollection
# \param[in/out] buffer
# \param[in/out] error
#
iiCopyObject(*itemParent, *itemName, *itemIsCollection, *buffer, *error) {
*sourcePath = "*itemParent/*itemName";
*destPath = *buffer.destination;
if (*sourcePath != *buffer."source") {
# rewrite path to copy objects that are located underneath the toplevel collection
*sourceLength = strlen(*sourcePath);
*relativePath = substr(*sourcePath, strlen(*buffer."source") + 1, *sourceLength);
*destPath = *buffer."destination" ++ "/" ++ *relativePath;
}
if (*itemIsCollection) {
*error = errorcode(msiCollCreate(*destPath, 1, *status));
if (*error < 0) {
*buffer.msg = "Failed to create collection *destPath";
}
} else {
*resource = "";
*numThreads = "";
*err1 = errorcode(rule_resource_research(*resource));
*err2 = errorcode(rule_vault_copy_numthreads(*numThreads));
*error = errorcode(msiDataObjCopy(*sourcePath, *destPath, "destRescName=" ++ *resource ++ "++++numThreads=" ++ *numThreads ++ "++++verifyChksum=", *status));
if (*error < 0) {
*buffer.msg = "Failed to copy *sourcePath to *destPath";
}
}
}
#\ Generic secure copy functionality
# \param[in] argv argument string for secure copy like "*publicHost inbox /var/www/landingpages/*publicPath";
# \param[in] origin_path local path of origin file
# \param[out] err return the error to calling function
#
iiGenericSecureCopy(*argv, *origin_path, *err) {
*intErr = errorcode(msiExecCmd("securecopy.sh", *argv, "", *origin_path, 1, *cmdExecOut));
*err = str(*intErr);
if (*intErr < 0 ) {
msiGetStderrInExecCmdOut(*cmdExecOut, *stderr);
msiGetStdoutInExecCmdOut(*cmdExecOut, *stdout);
writeLine("serverLog", "iiGenericSecureCopy: errorcode *err");
writeLine("serverLog", *stderr);
writeLine("serverLog", *stdout);
}
}
# \brief When inheritance is missing we need to copy ACL's when introducing new data in vault package.
#
# \param[in] path path of object that needs the permissions of parent
# \param[in] recursiveFlag either "default" for no recursion or "recursive"
#
iiCopyACLsFromParent(*path, *recursiveFlag) {
uuChopPath(*path, *parent, *child);
foreach(*row in SELECT COLL_ACCESS_NAME, COLL_ACCESS_USER_ID WHERE COLL_NAME = *parent) {
*accessName = *row.COLL_ACCESS_NAME;
*userId = *row.COLL_ACCESS_USER_ID;
*userFound = false;
foreach(*user in SELECT USER_NAME WHERE USER_ID = *userId) {
*userName = *user.USER_NAME;
*userFound = true;
}
if (*userFound) {
if (*accessName == "own") {
writeLine("serverLog", "iiCopyACLsFromParent: granting own to <*userName> on <*path> with recursiveFlag <*recursiveFlag>");
msiSetACL(*recursiveFlag, "own", *userName, *path);
} else if (*accessName == "read object") {
writeLine("serverLog", "iiCopyACLsFromParent: granting read to <*userName> on <*path> with recursiveFlag <*recursiveFlag>");
msiSetACL(*recursiveFlag, "read", *userName, *path);
} else if (*accessName == "modify object") {
writeLine("serverLog", "iiCopyACLsFromParent: granting write to <*userName> on <*path> with recursiveFlag <*recursiveFlag>");
msiSetACL(*recursiveFlag, "write", *userName, *path);
}
}
}
}
# \brief Copy a vault package to the research area.
#
# \param[in] folder folder to copy from the vault
# \param[in] target path of the research area target
#
iiCopyFolderToResearch(*folder, *target) {
writeLine("stdout", "iiCopyFolderToResearch: Copying *folder to *target.");
# Determine target collection group and actor.
*pathElems = split(*folder, "/");
*elemSize = size(*pathElems);
*vaultPackage = elem(*pathElems, *elemSize - 1);
*buffer.source = *folder;
*buffer.destination = *target ++ "/" ++ *vaultPackage;
uuTreeWalk("forward", *folder, "iiCopyObject", *buffer, *error);
if (*error != 0) {
msiGetValByKey(*buffer, "msg", *msg); # using . syntax here lead to type error
writeLine("stdout", "iiCopyObject: *error: *msg");
fail;
}
}
# \brief Retrieve current vault folder status
#
# \param[in] folder Path of vault folder
# \param[out] folderStatus Current status of vault folder
#
iiVaultStatus(*folder, *vaultStatus) {
*vaultStatusKey = IIVAULTSTATUSATTRNAME;
*vaultStatus = UNPUBLISHED;
foreach(*row in SELECT META_COLL_ATTR_VALUE WHERE COLL_NAME = *folder AND META_COLL_ATTR_NAME = *vaultStatusKey) {
*vaultStatus = *row.META_COLL_ATTR_VALUE;
}
}
# \brief Retrieve actor of action on vault folder
#
# \param[in] folder Path of action vault folder
# \param[out] actionActor Actor of action on vault folder
#
iiVaultGetActionActor(*folder, *actor, *actionActor) {
# Retrieve vault folder collection id.
foreach(*row in SELECT COLL_ID WHERE COLL_NAME = *folder) {
*collId = *row.COLL_ID;
}
# Retrieve vault folder action actor.
*actionActor = "";
foreach(*row in SELECT ORDER_DESC(META_COLL_MODIFY_TIME), COLL_ID, META_COLL_ATTR_VALUE WHERE META_COLL_ATTR_NAME = "org_vault_action_*collId") {
*err = errorcode(msi_json_arrayops(*row.META_COLL_ATTR_VALUE, *actionActor, "get", 2));
if (*err < 0) {
writeLine("serverLog", "iiVaultGetActionActor: org_vault_action_*collId contains invalid JSON");
} else {
writeLine("serverLog", "iiVaultGetActionActor: org_vault_action_*collId actor is *actionActor");
}
break;
}
# Fallback actor (rodsadmin).
if (*actionActor == "") {
*actionActor = *actor;
}
}
# \brief Perform admin operations on the vault
#
iiAdminVaultActions() {
msiExecCmd("admin-vaultactions.sh", uuClientFullName, "", "", 0, *out);
}
# \brief Prepare to archive a data package in the vault
#
iiAdminVaultArchive(*coll, *action) {
msiExecCmd("admin-vault-archive.sh", uuClientFullName ++ " " ++ *coll ++ " " ++ *action, "", "", 0, *out);
}