--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * @file CannotCreateFileException.h
+ * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @version 1.0
+ * @brief This file defines exception thrown when database file cannot be created
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_CANNOTCREATEFILEEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_CANNOTCREATEFILEEXCEPTION_H_
+
+#include <string>
+
+#include <exceptions/DatabaseException.h>
+
+namespace Cynara {
+
+class CannotCreateFileException : public DatabaseException {
+public:
+ CannotCreateFileException(const std::string &filename) : m_filename(filename) {};
+ virtual ~CannotCreateFileException() = default;
+
+ const std::string message(void) const {
+ if (m_message.empty()) {
+ m_message = "File " + filename() + " cannot be created";
+ }
+ return m_message;
+ }
+
+ const std::string &filename(void) const {
+ return m_filename;
+ }
+
+private:
+ mutable std::string m_message;
+ std::string m_filename;
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_EXCEPTIONS_CANNOTCREATEFILEEXCEPTION_H_ */
* @brief Methods implementation of Cynara::BucketDeserializer
*/
+#include <array>
+#include <memory>
+#include <string>
+#include <vector>
+
#include <exceptions/BucketRecordCorruptedException.h>
-#include <storage/BucketDeserializer.h>
+#include <types/PolicyCollection.h>
+#include <types/PolicyResult.h>
+#include <types/PolicyType.h>
+
#include <storage/StorageDeserializer.h>
#include <storage/StorageSerializer.h>
-#include <types/Policy.h>
-#include <iostream>
-#include <memory>
-#include <string>
+#include "BucketDeserializer.h"
namespace Cynara {
PolicyCollection policies;
// TODO: Get someone smart to do error checking on stream
- for(std::size_t lineNum = 1; !m_inStream.eof(); ++lineNum) {
+ for (std::size_t lineNum = 1; !m_inStream.eof(); ++lineNum) {
std::string line;
std::getline(m_inStream, line, StorageSerializer::recordSeparator());
PolicyKey BucketDeserializer::parseKey(const std::string &line, std::size_t &beginToken) {
std::array<std::string, 3> keyFeatures;
- for(std::size_t tokenNum = 0; tokenNum < keyFeatures.size(); ++tokenNum) {
+ for (std::size_t tokenNum = 0; tokenNum < keyFeatures.size(); ++tokenNum) {
auto endToken = line.find(StorageSerializer::fieldSeparator(), beginToken);
if (endToken != std::string::npos) {
keyFeatures[tokenNum] = line.substr(beginToken, endToken - beginToken);
#ifndef SRC_SERVICE_STORAGE_BUCKETDESERIALIZER_H_
#define SRC_SERVICE_STORAGE_BUCKETDESERIALIZER_H_
+#include <istream>
+#include <string>
+
#include <types/PolicyCollection.h>
#include <types/PolicyKey.h>
-#include <types/PolicyResult.h>
-#include <types/PolicyType.h>
-
-#include <istream>
namespace Cynara {
--- /dev/null
+/*
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * @file Buckets.h
+ * @author Aleksander Zdyb <a.zdyb@partner.samsung.com>
+ * @author Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @version 1.0
+ * @brief Headers for Buckets
+ */
+
+#ifndef SRC_SERVICE_STORAGE_BUCKETS_H_
+#define SRC_SERVICE_STORAGE_BUCKETS_H_
+
+#include <unordered_map>
+
+#include <types/PolicyBucket.h>
+#include <types/PolicyBucketId.h>
+
+namespace Cynara {
+
+typedef std::unordered_map<PolicyBucketId, PolicyBucket> Buckets;
+
+} /* namespace Cynara */
+
+#endif /* SRC_SERVICE_STORAGE_BUCKETS_H_ */
* @brief Implementation of InMemoryStorageBackend
*/
+#include <errno.h>
#include <fstream>
#include <functional>
+#include <string>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unordered_map>
+#include <vector>
#include <log/log.h>
+#include <exceptions/BucketNotExistsException.h>
+#include <exceptions/CannotCreateFileException.h>
#include <exceptions/FileNotFoundException.h>
+#include <exceptions/UnexpectedErrorException.h>
+#include <types/PolicyBucket.h>
+#include <types/PolicyResult.h>
+#include <types/PolicyType.h>
+
#include <storage/BucketDeserializer.h>
#include <storage/StorageDeserializer.h>
-#include <types/PolicyBucketId.h>
+#include <storage/StorageSerializer.h>
#include "InMemoryStorageBackend.h"
namespace Cynara {
void InMemoryStorageBackend::load(void) {
- std::string indexFilename = m_dbPath + "buckets";
+ std::string indexFilename = m_dbPath + m_indexFileName;
try {
std::ifstream indexStream;
LOGE("Reading cynara database failed.");
}
- if(!hasBucket(defaultPolicyBucketId)) {
+ if (!hasBucket(defaultPolicyBucketId)) {
LOGN("Creating defaultBucket.");
this->buckets().insert({ defaultPolicyBucketId, PolicyBucket() });
}
}
+void InMemoryStorageBackend::save(void) {
+
+ //create directory
+ int ret = mkdir(m_dbPath.c_str(), S_IRWXU);
+ if (ret < 0) {
+ int err = errno;
+ if (err != EEXIST) {
+ LOGE("Cannot create directory <%s>. Error [%d] : <%s>.",
+ m_dbPath.c_str(), err, strerror(err));
+ throw UnexpectedErrorException(err, strerror(err));
+ }
+ }
+
+ std::ofstream indexStream;
+ openDumpFileStream(indexStream, m_dbPath + m_indexFileName);
+
+ StorageSerializer storageSerializer(indexStream);
+ storageSerializer.dump(buckets(), std::bind(&InMemoryStorageBackend::bucketDumpStreamOpener,
+ this, std::placeholders::_1));
+}
+
PolicyBucket InMemoryStorageBackend::searchDefaultBucket(const PolicyKey &key) {
return searchBucket(defaultPolicyBucketId, key);
}
[key](PolicyPtr policy) -> bool {
return policy->key() == key;
}), policies.end());
- } catch(const std::out_of_range &) {
+ } catch (const std::out_of_range &) {
throw BucketNotExistsException(bucketId);
}
}
return false;
};
- for(auto &bucketIter : buckets()) {
+ for (auto &bucketIter : buckets()) {
// TODO: Move the erase code to PolicyCollection maybe?
auto &bucket = bucketIter.second;
auto &policies = bucket.policyCollection();
throw FileNotFoundException(filename);
}
+void InMemoryStorageBackend::openDumpFileStream(std::ofstream &stream,
+ const std::string &filename) {
+ stream.open(filename, std::ofstream::out | std::ofstream::trunc);
+
+ if (!stream.is_open()) {
+ throw CannotCreateFileException(filename);
+ return;
+ }
+}
+
std::shared_ptr<BucketDeserializer> InMemoryStorageBackend::bucketStreamOpener(
const PolicyBucketId &bucketId) {
std::string bucketFilename = m_dbPath + "_" + bucketId;
}
}
+std::shared_ptr<StorageSerializer> InMemoryStorageBackend::bucketDumpStreamOpener(
+ const PolicyBucketId &bucketId) {
+ std::string bucketFilename = m_dbPath + "_" + bucketId;
+ std::ofstream bucketStream;
+
+ openDumpFileStream(bucketStream, bucketFilename);
+ return std::make_shared<StorageSerializer>(bucketStream);
+}
+
} /* namespace Cynara */
* @brief Headers for InMemoryStorageBackend
*/
-#ifndef INMEMORYSTORAGEBACKEND_H_
-#define INMEMORYSTORAGEBACKEND_H_
+#ifndef SRC_SERVICE_STORAGE_INMEMORYSTORAGEBACKEND_H_
+#define SRC_SERVICE_STORAGE_INMEMORYSTORAGEBACKEND_H_
-#include <algorithm>
#include <fstream>
-#include <functional>
-#include <iostream>
-#include <unordered_map>
+#include <memory>
+#include <string>
-#include <exceptions/BucketNotExistsException.h>
-#include <exceptions/NotImplementedException.h>
-#include <storage/BucketDeserializer.h>
-#include <types/Policy.h>
+#include <types/pointers.h>
+#include <types/PolicyBucket.h>
+#include <types/PolicyBucketId.h>
+#include <types/PolicyKey.h>
+#include <types/PolicyResult.h>
-#include "StorageBackend.h"
+#include <storage/BucketDeserializer.h>
+#include <storage/Buckets.h>
+#include <storage/StorageBackend.h>
+#include <storage/StorageSerializer.h>
namespace Cynara {
class InMemoryStorageBackend : public StorageBackend {
public:
- typedef std::unordered_map<PolicyBucketId, PolicyBucket> Buckets;
-
InMemoryStorageBackend(const std::string &path) : m_dbPath(path) {
}
-
virtual ~InMemoryStorageBackend() = default;
virtual void load(void);
+ virtual void save(void);
virtual PolicyBucket searchDefaultBucket(const PolicyKey &key);
virtual PolicyBucket searchBucket(const PolicyBucketId &bucketId, const PolicyKey &key);
void openFileStream(std::ifstream &stream, const std::string &filename);
std::shared_ptr<BucketDeserializer> bucketStreamOpener(const PolicyBucketId &bucketId);
+ void openDumpFileStream(std::ofstream &stream, const std::string &filename);
+ std::shared_ptr<StorageSerializer> bucketDumpStreamOpener(const PolicyBucketId &bucketId);
+
private:
std::string m_dbPath;
Buckets m_buckets;
+ const std::string m_indexFileName = "buckets";
protected:
- virtual Buckets &buckets() {
+ virtual Buckets &buckets(void) {
return m_buckets;
}
};
} /* namespace Cynara */
-#endif /* INMEMORYSTORAGEBACKEND_H_ */
+
+#endif /* SRC_SERVICE_STORAGE_INMEMORYSTORAGEBACKEND_H_ */
* @brief This file implements policy rules storage procedures
*/
-#include "Storage.h"
-#include "StorageBackend.h"
-#include "types/pointers.h"
-#include "types/PolicyType.h"
-#include "exceptions/NotImplementedException.h"
-#include "exceptions/DefaultBucketDeletionException.h"
-
-#include <iostream>
#include <memory>
+#include <vector>
+#include "exceptions/DefaultBucketDeletionException.h"
+#include <types/pointers.h>
+#include <types/Policy.h>
+#include <types/PolicyBucket.h>
+#include <types/PolicyBucketId.h>
+#include <types/PolicyCollection.h>
+#include <types/PolicyResult.h>
+#include <types/PolicyType.h>
+
+#include "Storage.h"
namespace Cynara {
const auto &policyResult = policyRecord->result();
switch (policyResult.policyType()) {
- case PredefinedPolicyType::DENY:
- return policyResult; // Do not expect lower value than DENY
- break;
- case PredefinedPolicyType::BUCKET: {
- auto bucketResults = m_backend.searchBucket(policyResult.metadata(), key);
- auto minimumOfBucket = minimalPolicy(bucketResults, key);
- proposeMinimal(minimumOfBucket);
- continue;
- }
- break;
- case PredefinedPolicyType::ALLOW:
- default:
- break;
+ case PredefinedPolicyType::DENY:
+ return policyResult; // Do not expect lower value than DENY
+ case PredefinedPolicyType::BUCKET: {
+ auto bucketResults = m_backend.searchBucket(policyResult.metadata(), key);
+ auto minimumOfBucket = minimalPolicy(bucketResults, key);
+ proposeMinimal(minimumOfBucket);
+ continue;
+ }
+ case PredefinedPolicyType::ALLOW:
+ default:
+ break;
}
proposeMinimal(policyResult);
m_backend.load();
}
+void Storage::save(void) {
+ m_backend.save();
+}
+
} // namespace Cynara
* @brief This file is the implementation file of log system
*/
-#ifndef CYNARA_SERVICE_STORAGE_STORAGE_H
-#define CYNARA_SERVICE_STORAGE_STORAGE_H
+#ifndef SRC_SERVICE_STORAGE_STORAGE_H_
+#define SRC_SERVICE_STORAGE_STORAGE_H_
#include <map>
-#include <memory>
#include <string>
-#include <tuple>
#include <vector>
-#include "types/pointers.h"
-#include "types/PolicyBucketId.h"
-#include "types/PolicyResult.h"
-#include "types/PolicyKey.h"
+#include <types/Policy.h>
+#include <types/PolicyBucket.h>
+#include <types/PolicyBucketId.h>
+#include <types/PolicyKey.h>
+#include <types/PolicyResult.h>
-namespace Cynara {
+#include <storage/StorageBackend.h>
-class StorageBackend;
-class PolicyBucket;
+namespace Cynara {
class Storage
{
void deleteBucket(const PolicyBucketId &bucketId);
void load(void);
+ void save(void);
protected:
PolicyResult minimalPolicy(const PolicyBucket &bucket, const PolicyKey &key);
} // namespace Cynara
-#endif /* CYNARA_SERVICE_STORAGE_STORAGE_H */
+#endif /* SRC_SERVICE_STORAGE_STORAGE_H_ */
* @brief Headers for StorageBackend base class
*/
-#ifndef STORAGEBACKEND_H_
-#define STORAGEBACKEND_H_
+#ifndef SRC_SERVICE_STORAGE_STORAGEBACKEND_H_
+#define SRC_SERVICE_STORAGE_STORAGEBACKEND_H_
+#include <string>
-#include "types/pointers.h"
-#include "types/PolicyBucket.h"
-#include "types/PolicyBucketId.h"
-
-#include <memory>
+#include <types/pointers.h>
+#include <types/PolicyBucket.h>
+#include <types/PolicyKey.h>
+#include <types/PolicyBucketId.h>
+#include <types/PolicyResult.h>
namespace Cynara {
-class PolicyKey;
-
class StorageBackend {
public:
virtual ~StorageBackend() {}
virtual void insertPolicy(const PolicyBucketId &bucket, PolicyPtr policy) = 0;
- virtual void createBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultPolicy) = 0;
- virtual void updateBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultPolicy) = 0;
+ virtual void createBucket(const PolicyBucketId &bucketId,
+ const PolicyResult &defaultPolicy) = 0;
+ virtual void updateBucket(const PolicyBucketId &bucketId,
+ const PolicyResult &defaultPolicy) = 0;
virtual void deleteBucket(const PolicyBucketId &bucketId) = 0;
virtual bool hasBucket(const PolicyBucketId &bucketId) = 0;
virtual void deleteLinking(const PolicyBucketId &bucket) = 0;
virtual void load(void) = 0;
+ virtual void save(void) = 0;
};
} /* namespace Cynara */
-#endif /* STORAGEBACKEND_H_ */
+
+#endif /* SRC_SERVICE_STORAGE_STORAGEBACKEND_H_ */
* @brief Implementation for Cynara::StorageDeserializer
*/
-#include <iostream>
+#include <istream>
#include <string>
#include <exceptions/BucketDeserializationException.h>
#include <exceptions/BucketRecordCorruptedException.h>
+#include <types/PolicyType.h>
+
#include <storage/BucketDeserializer.h>
+#include <storage/Buckets.h>
#include <storage/StorageSerializer.h>
-#include <storage/StorageDeserializer.h>
+#include "StorageDeserializer.h"
namespace Cynara {
BucketStreamOpener bucketStreamOpener)
: m_inStream(inStream), m_bucketStreamOpener(bucketStreamOpener) {}
-void StorageDeserializer::initBuckets(InMemoryStorageBackend::Buckets &buckets) {
+void StorageDeserializer::initBuckets(Buckets &buckets) {
buckets.clear();
- for(std::size_t lineNum = 1; !m_inStream.eof(); ++lineNum) {
+ for (std::size_t lineNum = 1; !m_inStream.eof(); ++lineNum) {
std::string line;
std::getline(m_inStream, line, StorageSerializer::recordSeparator());
}
}
-void StorageDeserializer::loadBuckets(InMemoryStorageBackend::Buckets &buckets) {
+void StorageDeserializer::loadBuckets(Buckets &buckets) {
for (auto &bucketIter : buckets) {
const auto &bucketId = bucketIter.first;
auto &bucket = bucketIter.second;
std::size_t newBegin = 0;
policyType = std::stoi(line.substr(beginToken), &newBegin, 16);
beginToken += newBegin;
- } catch(...) {
+ } catch (...) {
throw BucketRecordCorruptedException(line);
}
#ifndef SRC_SERVICE_STORAGE_STORAGEDESERIALIZER_H_
#define SRC_SERVICE_STORAGE_STORAGEDESERIALIZER_H_
-#include <storage/BucketDeserializer.h>
-#include <storage/InMemoryStorageBackend.h>
-
+#include <functional>
#include <istream>
#include <memory>
-#include <functional>
+#include <string>
+
+#include <types/PolicyBucketId.h>
+#include <types/PolicyResult.h>
+#include <types/PolicyType.h>
+
+#include <storage/BucketDeserializer.h>
+#include <storage/Buckets.h>
namespace Cynara {
class StorageDeserializer {
public:
- typedef std::function<std::shared_ptr<BucketDeserializer>(const std::string &)> BucketStreamOpener;
+ typedef std::function<std::shared_ptr<BucketDeserializer>(const std::string &)>
+ BucketStreamOpener;
StorageDeserializer(std::istream &inStream, BucketStreamOpener m_bucketStreamOpener);
- void initBuckets(InMemoryStorageBackend::Buckets &buckets);
- void loadBuckets(InMemoryStorageBackend::Buckets &buckets);
+ void initBuckets(Buckets &buckets);
+ void loadBuckets(Buckets &buckets);
static PolicyBucketId parseBucketId(const std::string &line, std::size_t &beginToken);
static PolicyType parsePolicyType(const std::string &line, std::size_t &beginToken);
* @brief Implementation of Cynara::StorageSerializer methods
*/
-#include <algorithm>
#include <ios>
-#include <iostream>
+#include <ostream>
#include <exceptions/BucketSerializationException.h>
-#include "types/PolicyBucket.h"
-#include "types/PolicyCollection.h"
+#include <types/Policy.h>
+#include <types/PolicyBucket.h>
+#include <types/PolicyBucketId.h>
+#include <types/PolicyCollection.h>
+#include <types/PolicyType.h>
#include "StorageSerializer.h"
StorageSerializer::StorageSerializer(std::ostream &os) : m_outStream(os) {}
-void StorageSerializer::dump(const InMemoryStorageBackend::Buckets &buckets,
+void StorageSerializer::dump(const Buckets &buckets,
BucketStreamOpener streamOpener) {
for (const auto bucketIter : buckets) {
#ifndef SRC_SERVICE_STORAGE_STORAGESERIALIZER_H_
#define SRC_SERVICE_STORAGE_STORAGESERIALIZER_H_
-#include "InMemoryStorageBackend.h"
-#include "types/PolicyBucketId.h"
-#include "types/PolicyCollection.h"
-#include "types/PolicyResult.h"
-
+#include <functional>
+#include <memory>
#include <ostream>
-namespace Cynara {
+#include <types/PolicyBucket.h>
+#include <types/PolicyBucketId.h>
+#include <types/PolicyCollection.h>
+#include <types/PolicyKey.h>
+#include <types/PolicyResult.h>
+#include <types/PolicyType.h>
-class PolicyBucket;
-class PolicyKey;
+#include <storage/Buckets.h>
+
+namespace Cynara {
class StorageSerializer {
StorageSerializer(std::ostream &os);
virtual ~StorageSerializer() = default;
- virtual void dump(const InMemoryStorageBackend::Buckets &buckets,
+ virtual void dump(const Buckets &buckets,
BucketStreamOpener streamOpener);
virtual void dump(const PolicyBucket &bucket);