From 054d891297db613d7d2e4638a7e5801981c8719f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Alexandre=20D=27Abruzzo=20Pereira?= Date: Wed, 18 Dec 2019 17:20:04 +0000 Subject: [PATCH] CloudEA Integration (QM, Rules, Actuator) (#22) * Add Class to Calculate Score of CloudEA Quality Model * SecurityScore implemented * Adjustment according to the Score interface. * Changes in SecurityScore * Added the Scores (SecurityScore and PrivacyScore) to TrustworthinessScore * Adjusted the tmalibrary (actuator) to work properly with Python3 * Python Actuator Library Fix * Updated submodules. --- common/tma-utils/pom.xml | 2 +- .../tma/database/DatabaseManager.java | 2 +- .../qualitymodel/CompositeAttributeView.java | 5 + .../entity/qualitymodel/LeafAttribute.java | 24 +- .../atmosphere/tma/utils/PrivacyScore.java | 7 + .../atmosphere/tma/utils/SecurityScore.java | 417 ++++++++++++++++++ .../tma/utils/TrustworthinessScore.java | 32 +- common/tmalibrary/setup.py | 2 +- .../tmalibrary/actuator/ActuatorPayload.py | 5 +- .../tmalibrary/actuator/HandleRequest.py | 8 +- .../tmalibrary/actuator/KeyManager.py | 9 +- tma-framework-a | 2 +- tma-framework-e | 2 +- tma-framework-k | 2 +- tma-framework-m | 2 +- tma-framework-p | 2 +- 16 files changed, 503 insertions(+), 20 deletions(-) create mode 100644 common/tma-utils/src/main/java/eubr/atmosphere/tma/utils/SecurityScore.java diff --git a/common/tma-utils/pom.xml b/common/tma-utils/pom.xml index 2bea3cd..5e190eb 100644 --- a/common/tma-utils/pom.xml +++ b/common/tma-utils/pom.xml @@ -23,7 +23,7 @@ mysql mysql-connector-java - 5.1.6 + 8.0.15 diff --git a/common/tma-utils/src/main/java/eubr/atmosphere/tma/database/DatabaseManager.java b/common/tma-utils/src/main/java/eubr/atmosphere/tma/database/DatabaseManager.java index b7d8aea..5bff19c 100644 --- a/common/tma-utils/src/main/java/eubr/atmosphere/tma/database/DatabaseManager.java +++ b/common/tma-utils/src/main/java/eubr/atmosphere/tma/database/DatabaseManager.java @@ -30,7 +30,7 @@ public static Connection getConnectionInstance() { if ((connection == null) || connection.isClosed()) { connection = DriverManager .getConnection("jdbc:mysql://mysql-0.mysql.default.svc.cluster.local:3306/knowledge?" - + "user=root&password=passtobereplaced"); + + "user=root&password=passtobereplaced&autoReconnect=true"); } } catch (SQLException e) { LOGGER.error(e.getMessage()); diff --git a/common/tma-utils/src/main/java/eubr/atmosphere/tma/entity/qualitymodel/CompositeAttributeView.java b/common/tma-utils/src/main/java/eubr/atmosphere/tma/entity/qualitymodel/CompositeAttributeView.java index 5b4e0d7..f781b0d 100644 --- a/common/tma-utils/src/main/java/eubr/atmosphere/tma/entity/qualitymodel/CompositeAttributeView.java +++ b/common/tma-utils/src/main/java/eubr/atmosphere/tma/entity/qualitymodel/CompositeAttributeView.java @@ -1,6 +1,8 @@ package eubr.atmosphere.tma.entity.qualitymodel; import java.io.Serializable; +import java.sql.Timestamp; +import java.time.Instant; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -70,6 +72,9 @@ public MetricData calculate(ConfigurationProfile profile, Date timestamp) throws } MetricDataPK metricDataPK = new MetricDataPK(); + if (timestamp == null) { + timestamp = new Timestamp(Instant.now().toEpochMilli()); + } metricDataPK.setValueTime(timestamp); metricDataPK.setMetricId(this.getId()); metricData.setMetricId(metricDataPK); diff --git a/common/tma-utils/src/main/java/eubr/atmosphere/tma/entity/qualitymodel/LeafAttribute.java b/common/tma-utils/src/main/java/eubr/atmosphere/tma/entity/qualitymodel/LeafAttribute.java index a94d630..c4bf6f9 100644 --- a/common/tma-utils/src/main/java/eubr/atmosphere/tma/entity/qualitymodel/LeafAttribute.java +++ b/common/tma-utils/src/main/java/eubr/atmosphere/tma/entity/qualitymodel/LeafAttribute.java @@ -1,6 +1,8 @@ package eubr.atmosphere.tma.entity.qualitymodel; import java.io.Serializable; +import java.sql.Timestamp; +import java.time.Instant; import java.util.Date; import java.util.Iterator; import java.util.List; @@ -84,6 +86,9 @@ public MetricData calculate(ConfigurationProfile profile, Date timestamp) } MetricDataPK metricDataPK = new MetricDataPK(); + if (timestamp == null) { + timestamp = new Timestamp(Instant.now().toEpochMilli()); + } metricDataPK.setValueTime(timestamp); metricDataPK.setMetricId(this.getId()); metricData.setMetricId(metricDataPK); @@ -125,6 +130,10 @@ protected double calculateAverage(ConfigurationProfile profile, MetricData metri } } + if (average == 0 || amount == 0) { + return 0; + } + if (this.normalizationKind == MetricNormalizationKind.COST) { return 1 - (average / amount); } @@ -158,6 +167,10 @@ protected double calculateMinimum(ConfigurationProfile profile, MetricData metri } } + if (minimum == 0) { + return 0; + } + if (this.normalizationKind == MetricNormalizationKind.COST) { return 1 - minimum; } @@ -191,6 +204,10 @@ protected double calculateMaximum(ConfigurationProfile profile, MetricData metri } } + if (maximum == 0) { + return 0; + } + if (this.normalizationKind == MetricNormalizationKind.COST) { return 1 - maximum; } @@ -223,7 +240,10 @@ protected double calculateSum(ConfigurationProfile profile, MetricData metricDat } } - + if (sum == 0) { + return 0; + } + if (this.normalizationKind == MetricNormalizationKind.COST) { return 1 - sum; } @@ -286,4 +306,4 @@ public void setNumSamples(int numSamples) { this.numSamples = numSamples; } -} \ No newline at end of file +} diff --git a/common/tma-utils/src/main/java/eubr/atmosphere/tma/utils/PrivacyScore.java b/common/tma-utils/src/main/java/eubr/atmosphere/tma/utils/PrivacyScore.java index 275f71b..3503964 100644 --- a/common/tma-utils/src/main/java/eubr/atmosphere/tma/utils/PrivacyScore.java +++ b/common/tma-utils/src/main/java/eubr/atmosphere/tma/utils/PrivacyScore.java @@ -15,6 +15,13 @@ public class PrivacyScore implements Score { private int resourceId; private long valueTime; + public PrivacyScore() { + this.configurationProfileId = -1; + this.attributeId = -1; + this.score = 0.0; + this.threshold = 0.0; + } + public PrivacyScore(Integer configurationProfileId, Integer attributeId, Double score, Double threshold) { super(); this.score = score; diff --git a/common/tma-utils/src/main/java/eubr/atmosphere/tma/utils/SecurityScore.java b/common/tma-utils/src/main/java/eubr/atmosphere/tma/utils/SecurityScore.java new file mode 100644 index 0000000..70742e8 --- /dev/null +++ b/common/tma-utils/src/main/java/eubr/atmosphere/tma/utils/SecurityScore.java @@ -0,0 +1,417 @@ +package eubr.atmosphere.tma.utils; + +import java.util.HashMap; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SecurityScore implements Score { + + private static final Logger LOGGER = LoggerFactory.getLogger(SecurityScore.class); + + private Double score; + private int resourceId; + private long timestamp; + private int metricId; + + // Constructor of SecurityScore + public SecurityScore() { + super(); + } + + /** + * The following HashMaps store data required for calculation of Security Score. + * each HashMap maps desscriptionId with a value. It stores the values for + * several descriptions whose values are required for calculating one specific + * metric. As an example, existenceOfBestPractice stores the values for + * existence of best practices within each policy in order to be used for + * calculating Compliance with Vendor Best Practices (VBP). + */ + private HashMap existenceOfBestPractice = new HashMap(); + private HashMap existenceOfCheckAreas = new HashMap(); + private HashMap existenceOfPolicy = new HashMap(); + private HashMap existenceOfSecurityControl = new HashMap(); + private HashMap existenceOfSecurityDefinition = new HashMap(); + + // metrics used for calculation of security Score + + private double complianceWithVendorBestPractices_VBP; + private static final int numberOfPolicies = 3; + + private double complianceWithIndustryDefinedConfiguration_SIC; + + private double complianceWithAllSecurityStandards_SS; + private static final int numberOfStandards = 6; + + private double totalSecurityPoliciesInPlace_SP; + private static final int numberOfTechnologies = 6; + + private double totalSecurityCoverage_SC; + + private double availabilityLevel_A; + private double integrityLevel_I; + private double confidentialityLevel_C; + + ///////////////////////// weights of attributes and sub-attributes + + private double[] S_AttributesWeights; + private double[] A_AttributesWeights; + private double[] I_AttributesWeights; + private double[] C_AttributesWeights; + private double[] SCT_AttributesWeights; + private double[] SC_AttributesWeights; + private double[] P_AttributesWeights; + private double[] SP_AttributesWeights; + private double[] SS_AttributesWeights; + private double[] CSS_AttributesWeiths; + private double[] SIC_AttributesWeights; + private double[] SICP_AttributesWeights; + private double[] VBP_AttributesWeights; + private double[] VBPP_AttributesWeights; + + // getter and setters + + public Double getScore() { + // verifies whether is score has already been calculated or not. + if (this.score == null) + this.calculateScore(); + return this.score; + } + + public void setExistenceOfBestPractice(int resourceId, double existenceOfBestPractice) { + this.existenceOfBestPractice.put(resourceId, existenceOfBestPractice); + } + + public void setExistenceOfCheckAreas(int resourceId, double existenceOfCheckAreas) { + this.existenceOfCheckAreas.put(resourceId, existenceOfCheckAreas); + } + + public void setExistenceOfPolicy(int resourceId, double existenceOfPolicy) { + this.existenceOfPolicy.put(resourceId, existenceOfPolicy); + } + + public void setExistenceOfSecurityControl(int resourceId, double existenceOfSecurityControl) { + this.existenceOfSecurityControl.put(resourceId, existenceOfSecurityControl); + } + + public void setExistenceOfSecurityDefinition(int resourceId, double existenceOfSecurityDefinition) { + this.existenceOfSecurityDefinition.put(resourceId, existenceOfSecurityDefinition); + } + + //////// setters and getters for attributes weights + public double[] getS_AttributesWeights() { + return S_AttributesWeights; + } + + public void setS_AttributesWeights(double[] s_AttributesWeights) { + S_AttributesWeights = s_AttributesWeights; + } + + public double[] getA_AttributesWeights() { + return A_AttributesWeights; + } + + public void setA_AttributesWeights(double[] a_AttributesWeights) { + A_AttributesWeights = a_AttributesWeights; + } + + public double[] getI_AttributesWeights() { + return I_AttributesWeights; + } + + public void setI_AttributesWeights(double[] i_AttributesWeights) { + I_AttributesWeights = i_AttributesWeights; + } + + public double[] getC_AttributesWeights() { + return C_AttributesWeights; + } + + public void setC_AttributesWeights(double[] c_AttributesWeights) { + C_AttributesWeights = c_AttributesWeights; + } + + public double[] getSCT_AttributesWeights() { + return SCT_AttributesWeights; + } + + public void setSCT_AttributesWeights(double[] sCT_AttributesWeights) { + SCT_AttributesWeights = sCT_AttributesWeights; + } + + public double[] getSC_AttributesWeights() { + return SC_AttributesWeights; + } + + public void setSC_AttributesWeights(double[] sC_AttributesWeights) { + SC_AttributesWeights = sC_AttributesWeights; + } + + public double[] getP_AttributesWeights() { + return P_AttributesWeights; + } + + public void setP_AttributesWeights(double[] p_AttributesWeights) { + P_AttributesWeights = p_AttributesWeights; + } + + public double[] getSP_AttributesWeights() { + return SP_AttributesWeights; + } + + public void setSP_AttributesWeights(double[] sP_AttributesWeights) { + SP_AttributesWeights = sP_AttributesWeights; + } + + public double[] getSS_AttributesWeights() { + return SS_AttributesWeights; + } + + public void setSS_AttributesWeights(double[] sS_AttributesWeights) { + SS_AttributesWeights = sS_AttributesWeights; + } + + public double[] getCSS_AttributesWeiths() { + return CSS_AttributesWeiths; + } + + public void setCSS_AttributesWeiths(double[] cSS_AttributesWeiths) { + CSS_AttributesWeiths = cSS_AttributesWeiths; + } + + public double[] getSIC_AttributesWeights() { + return SIC_AttributesWeights; + } + + public void setSIC_AttributesWeights(double[] sIC_AttributesWeights) { + SIC_AttributesWeights = sIC_AttributesWeights; + } + + public double[] getSICP_AttributesWeights() { + return SICP_AttributesWeights; + } + + public void setSICP_AttributesWeights(double[] sICP_AttributesWeights) { + SICP_AttributesWeights = sICP_AttributesWeights; + } + + public double[] getVBP_AttributesWeights() { + return VBP_AttributesWeights; + } + + public void setVBP_AttributesWeights(double[] vBP_AttributesWeights) { + VBP_AttributesWeights = vBP_AttributesWeights; + } + + public double[] getVBPP_AttributesWeights() { + return VBPP_AttributesWeights; + } + + public void setVBPP_AttributesWeights(double[] vBPP_AttributesWeights) { + VBPP_AttributesWeights = vBPP_AttributesWeights; + } + + /////// getters for the value of each metric + public double getComplianceWithVendorBestPractices() { + return complianceWithVendorBestPractices_VBP; + } + + public double getComplianceWithIndustryDefinedConfiguration() { + return complianceWithIndustryDefinedConfiguration_SIC; + } + + public double getComplianceWithAllSecurityStandards() { + return complianceWithAllSecurityStandards_SS; + } + + public double getTotalSecurityPoliciesInPlace() { + return totalSecurityPoliciesInPlace_SP; + } + + public double getTotalSecurityCoverage() { + return totalSecurityCoverage_SC; + } + + public double getAvailabilityLevel() { + return availabilityLevel_A; + } + + public double getIntegrityLevel() { + return integrityLevel_I; + } + + public double getConfidentialityLevel() { + return confidentialityLevel_C; + } + + @Override + public String toString() { + this.calculateScore(); + return "SecurityScore [Final Score= " + this.score + ", Availability Score= " + this.availabilityLevel_A + + ", Integrity Score= " + this.integrityLevel_I + ", Confidentiality Score= " + + this.confidentialityLevel_C + ", Compliance With Security Standards= " + + this.complianceWithAllSecurityStandards_SS + ", Compliance With Industry Defined Configuration= " + + this.complianceWithIndustryDefinedConfiguration_SIC + ", Compliance With Vendor Best Practices= " + + this.complianceWithVendorBestPractices_VBP + ", Total Security Coverage= " + + this.totalSecurityCoverage_SC + ", Total Security Policies In Place= " + + this.totalSecurityPoliciesInPlace_SP + ", timestamp=" + System.currentTimeMillis() + "]"; + } + + /** + * This methods calculates the security score using the data, collected from + * several resources, regarding the attributes and sub-attributes are involved + * in the calculation of the score. There are several metrics and sub-metrics + * involved in the calculation of the score that have to be calculated before + * the score is calculated. + */ + private void calculateScore() { + // verifies whether all data required are provided or not + if (existenceOfBestPractice.isEmpty() || existenceOfSecurityDefinition.isEmpty() + || existenceOfCheckAreas.isEmpty() || existenceOfPolicy.isEmpty() + || existenceOfSecurityControl.isEmpty()) { + + LOGGER.info("Some data is missing for calculation of the score!"); + return; + } + + int bestPractices[][] = splitDigits(existenceOfBestPractice, numberOfPolicies); + int securityDefinitions[][] = splitDigits(existenceOfSecurityDefinition, numberOfPolicies); + int checkAreas[][] = splitDigits(existenceOfCheckAreas, numberOfStandards); + int policies[][] = splitDigits(existenceOfPolicy, numberOfTechnologies); + int securityControls[][] = splitDigits(existenceOfSecurityControl, numberOfTechnologies); + + // calculate complianceWithVendorBestPractices_VBP + this.complianceWithVendorBestPractices_VBP = calculateMetric(numberOfPolicies, bestPractices, + this.VBPP_AttributesWeights, this.VBP_AttributesWeights); + + // calculate complianceWithIndustryDefinedConfiguration_SIC + this.complianceWithIndustryDefinedConfiguration_SIC = calculateMetric(numberOfPolicies, securityDefinitions, + this.SICP_AttributesWeights, this.SIC_AttributesWeights); + + // calculate complianceWithAllSecurityStandards_SS + this.complianceWithAllSecurityStandards_SS = calculateMetric(numberOfStandards, checkAreas, + this.CSS_AttributesWeiths, this.SS_AttributesWeights); + + // calculate totalSecurityPoliciesInPlace_SP + this.totalSecurityPoliciesInPlace_SP = calculateMetric(numberOfTechnologies, policies, this.P_AttributesWeights, + this.SP_AttributesWeights); + + // calculate totalSecurityCoverage_SC + this.totalSecurityCoverage_SC = calculateMetric(numberOfTechnologies, securityControls, + this.SCT_AttributesWeights, this.SC_AttributesWeights); + + // calculate availabilityLevel_A + if (this.A_AttributesWeights == null) + this.A_AttributesWeights = setDefaultWeights(3); + + this.availabilityLevel_A = this.totalSecurityCoverage_SC * this.A_AttributesWeights[0] + + this.complianceWithIndustryDefinedConfiguration_SIC * this.A_AttributesWeights[1] + + this.complianceWithVendorBestPractices_VBP * this.A_AttributesWeights[2]; + + // calculate integrityLevel_I; + this.integrityLevel_I = this.complianceWithAllSecurityStandards_SS; + + // calculate confidentialityLevel_C; + if (this.C_AttributesWeights == null) + this.C_AttributesWeights = setDefaultWeights(2); + + this.confidentialityLevel_C = this.totalSecurityCoverage_SC * this.C_AttributesWeights[0] + + this.totalSecurityPoliciesInPlace_SP * this.C_AttributesWeights[1]; + + // calculate final score + if (this.S_AttributesWeights == null) + this.S_AttributesWeights = setDefaultWeights(3); + + this.score = this.availabilityLevel_A * S_AttributesWeights[0] + this.integrityLevel_I * S_AttributesWeights[1] + + this.confidentialityLevel_C * S_AttributesWeights[2]; + } + + /** + * This method calculates the value of each metric that is required for + * calculation of score. To do so, the values of attributes and sub-attributes + */ + private double calculateMetric(int numOfAttributes, int[][] ArrtibutesValues, double[] SubAttributesWeights, + double[] AttributesWeights) { + + if (SubAttributesWeights == null) + SubAttributesWeights = setDefaultWeights(ArrtibutesValues[0].length); + if (AttributesWeights == null) + AttributesWeights = setDefaultWeights(numOfAttributes); + + double metric = 0.0; + double[] subattribute = new double[numOfAttributes]; + for (int i = 0; i < numOfAttributes; i++) { + for (int j = 0; j < ArrtibutesValues[i].length; j++) + subattribute[i] += ArrtibutesValues[i][j] * SubAttributesWeights[j]; + + metric += subattribute[i] * AttributesWeights[i]; + } + + return metric; + } + + /** + * In the case no weight has been assigned to the attributes and sub-attributes, + * an equal weight is assigned to each. + */ + private double[] setDefaultWeights(int numOfAtt) { + double[] weights = new double[numOfAtt]; + double w = 1.0 / numOfAtt; + for (int i = 0; i < numOfAtt; i++) + weights[i] = w; + return weights; + } + + /** + * This method is responsible for splitting the value provided for each resource + * that includes collected data for several attributes. For instance, existence + * of security definition x is a resource whose value represents the existence + * of this security definition in various technologies (which are attributes) in + * a sequence of 0/1. + */ + private static int[][] splitDigits(HashMap values, int numberOfAttributes) { + + int numberOfSubAatributes = values.size(); + int[][] digits = new int[numberOfAttributes][numberOfSubAatributes]; + + int subAttributeCounter = 0; + for (Double val : values.values()) { + for (int i = 0; i < numberOfAttributes; i++) { + digits[i][subAttributeCounter] = (int) (val % 10.0); + val = val / 10.0; + } + subAttributeCounter++; + } + + return digits; + } + + public void setMetricId(int id) { + this.metricId = id; + } + + public void setValueTime(long l) { + this.timestamp = l; + + } + + public void setResourceId(int id) { + this.resourceId = id; + } + + @Override + public long getValueTime() { + return this.timestamp; + } + + @Override + public int getResourceId() { + return this.resourceId; + } + + @Override + public int getMetricId() { + return this.metricId; + } + +} diff --git a/common/tma-utils/src/main/java/eubr/atmosphere/tma/utils/TrustworthinessScore.java b/common/tma-utils/src/main/java/eubr/atmosphere/tma/utils/TrustworthinessScore.java index a3b8b86..9baebaf 100644 --- a/common/tma-utils/src/main/java/eubr/atmosphere/tma/utils/TrustworthinessScore.java +++ b/common/tma-utils/src/main/java/eubr/atmosphere/tma/utils/TrustworthinessScore.java @@ -5,6 +5,8 @@ public class TrustworthinessScore implements Score { private Double score; private ResourceConsumptionScore resourceConsumptionScore; private PerformanceScore performanceScore; + private SecurityScore securityScore; + private PrivacyScore privacyScore; private Integer podCount; private int metricId; private int resourceId; @@ -14,6 +16,26 @@ public TrustworthinessScore(ResourceConsumptionScore resourceConsumptionPodScore PerformanceScore performanceScore) { this.resourceConsumptionScore = resourceConsumptionPodScore; this.performanceScore = performanceScore; + this.securityScore = new SecurityScore(); + this.privacyScore = new PrivacyScore(); + this.podCount = 0; + this.score = 0.0; + } + + public TrustworthinessScore(PrivacyScore privacyScore) { + this.privacyScore = privacyScore; + this.resourceConsumptionScore = new ResourceConsumptionScore(); + this.performanceScore = new PerformanceScore(); + this.securityScore = new SecurityScore(); + this.podCount = 0; + this.score = 0.0; + } + + public TrustworthinessScore(SecurityScore securityScore) { + this.securityScore = securityScore; + this.resourceConsumptionScore = new ResourceConsumptionScore(); + this.performanceScore = new PerformanceScore(); + this.privacyScore = new PrivacyScore(); this.podCount = 0; this.score = 0.0; } @@ -61,6 +83,14 @@ public PerformanceScore getPerformanceScore() { return this.performanceScore; } + public PrivacyScore getPrivacyScore() { + return this.privacyScore; + } + + public SecurityScore getSecurityScore() { + return this.securityScore; + } + @Override public long getValueTime() { return this.valueTime; @@ -78,7 +108,7 @@ public int getResourceId() { public void setResourceId(int resourceId) { this.resourceId = resourceId; } - + @Override public int getMetricId() { return this.metricId; diff --git a/common/tmalibrary/setup.py b/common/tmalibrary/setup.py index 6e42da3..7dd38a4 100644 --- a/common/tmalibrary/setup.py +++ b/common/tmalibrary/setup.py @@ -5,7 +5,7 @@ setup( name='tmalibrary', - version='7.0', + version='9.12', author='Rui Silva', author_email='rfsilva@student.dei.uc.pt', description='Probe and Actuator Library', diff --git a/common/tmalibrary/tmalibrary/actuator/ActuatorPayload.py b/common/tmalibrary/tmalibrary/actuator/ActuatorPayload.py index 8d71c55..a492be9 100644 --- a/common/tmalibrary/tmalibrary/actuator/ActuatorPayload.py +++ b/common/tmalibrary/tmalibrary/actuator/ActuatorPayload.py @@ -10,4 +10,7 @@ def compute_attr_value(self,value): elif type(value) is dict: return ActuatorPayload(value) else: - return value \ No newline at end of file + return value + + def __getitem__(self, item): + return self.__dict__[item] diff --git a/common/tmalibrary/tmalibrary/actuator/HandleRequest.py b/common/tmalibrary/tmalibrary/actuator/HandleRequest.py index 54c8aee..fb441de 100644 --- a/common/tmalibrary/tmalibrary/actuator/HandleRequest.py +++ b/common/tmalibrary/tmalibrary/actuator/HandleRequest.py @@ -2,6 +2,8 @@ import base64 import json from .ActuatorPayload import ActuatorPayload +import os +from flask import Response class HandleRequest: @@ -17,10 +19,8 @@ def generateResponse(self, plainResponse): publicKeyExecutorPath = "keys/pub-key-executor" publicKeyExecutor = keymanager.getPublicKey(publicKeyExecutorPath) encryptedMessage = keymanager.encrypt(plainResponse,publicKeyExecutor) - response = base64.b64encode(str(encryptedMessage)) - response = response + "\n" - response = response + signedResponseEncoded - return response + response = base64.b64encode(encryptedMessage) + return Response("{}\n{}".format(response, signedResponseEncoded), mimetype='application/octet-stream') def processRequest(self, request): diff --git a/common/tmalibrary/tmalibrary/actuator/KeyManager.py b/common/tmalibrary/tmalibrary/actuator/KeyManager.py index 079a77f..fae8c38 100644 --- a/common/tmalibrary/tmalibrary/actuator/KeyManager.py +++ b/common/tmalibrary/tmalibrary/actuator/KeyManager.py @@ -23,13 +23,13 @@ def encrypt(self, text, public_key_string): public_key = RSA.importKey(public_key_string) # encrypt the plain text using the public key - encryptedText = public_key.encrypt(text,random.randint(1,101)) + encryptedText = public_key.encrypt(text.encode('utf-8'), 32)[0] return encryptedText def getPrivateKey(self, filenameprivatekey): # read private key from file try: - privKey = open(filenameprivatekey,"r").read() + privKey = open(filenameprivatekey, "rb").read() return privKey except EnvironmentError as e: @@ -39,7 +39,7 @@ def getPrivateKey(self, filenameprivatekey): def getPublicKey(self, filenamepublickey): # read public key from file try: - pubkey = open(filenamepublickey,"r").read() + pubkey = open(filenamepublickey, "rb").read() return pubkey except EnvironmentError as e: print(os.strerror(e.errno)) @@ -48,7 +48,8 @@ def getPublicKey(self, filenamepublickey): # The method that signs the data using the private key that is stored in keyFile path def sign(self, data,keyFile): privateSignature = RSA.importKey(keyFile) - h = SHA.new(data) + encData = data.encode('utf-8') + h = SHA.new(encData) signer = Crypto.Signature.PKCS1_v1_5.new(privateSignature) signature = signer.sign(h) return signature diff --git a/tma-framework-a b/tma-framework-a index e17d97d..cf1998a 160000 --- a/tma-framework-a +++ b/tma-framework-a @@ -1 +1 @@ -Subproject commit e17d97db06dc11c664bd211161c1f68fab7a13a8 +Subproject commit cf1998a218cd32a5c859a515b31fd1eee3f8efb8 diff --git a/tma-framework-e b/tma-framework-e index d11f608..623d2a0 160000 --- a/tma-framework-e +++ b/tma-framework-e @@ -1 +1 @@ -Subproject commit d11f608ecd3ea11bbbac81629d736c846e743578 +Subproject commit 623d2a0762d39e3ebde5ac4aaa4eadc5b166d691 diff --git a/tma-framework-k b/tma-framework-k index 30359fa..f074d57 160000 --- a/tma-framework-k +++ b/tma-framework-k @@ -1 +1 @@ -Subproject commit 30359fab1932370141660c28904e3c57c0f0ff67 +Subproject commit f074d57e1101d7ae81354b6cdfd667aa5a0d417e diff --git a/tma-framework-m b/tma-framework-m index b44703c..30d87bc 160000 --- a/tma-framework-m +++ b/tma-framework-m @@ -1 +1 @@ -Subproject commit b44703c6237091f3d1eff8f6f44df237d9c77050 +Subproject commit 30d87bc67c1b8625ea9b7763486435c6e32a43d2 diff --git a/tma-framework-p b/tma-framework-p index 6689440..172c665 160000 --- a/tma-framework-p +++ b/tma-framework-p @@ -1 +1 @@ -Subproject commit 668944089bcc2b603edbf50e873047fff825c4ae +Subproject commit 172c665d3c19c391a260e479ffc70b2f78ac842d