def pushUpdates(self,secretId,secretString): appsyncClient = AppSyncClient(authenticationType="API_KEY") secretString = secretString.replace("\"","\\\"") query = json.dumps({"query": "mutation {\n updateSecret(id:\""+secretId+"\",secretString:\""+secretString+"\") {\n id\n secretString\n }\n}\n"}) response = appsyncClient.execute(data=query) logger.info({ "response" : response })
class RealTimeS3(): def __init__(self, **kargs): self.region = kargs.get("region") self.bucketId = kargs.get("bucketId") or config.getProperty("bucketId") self.objectKey = kargs.get("objectKey") or config.getProperty( "objectKey") self.apiId = kargs.get("apiId") or config.getProperty("apiId") if (self.bucketId == None or self.objectKey == None or self.region == None): logger.error("region,bucketId and objectKey required") raise Exception("configuration error") self.client = AppSyncClient(region=self.region, apiId=self.apiId) def getValue(self): client = boto3.client("s3", region_name=self.region) file = client.get_object(Bucket=self.bucketId, Key=self.objectKey) filecontent = file.get("Body").read().decode("utf-8") return str(filecontent) def subscribe(self, callback): contents = self.getValue() callback(contents) id = "arn:aws:s3:::" + self.bucketId + ":" + self.objectKey query = json.dumps({ "query": "subscription {\n updatedResource(id:\"" + id + "\") {\n id\n data\n }\n}\n" }) def secretcallback(client, userdata, msg): logger.debug("New data received : " + str(msg)) callbackdatab64 = json.loads(msg.payload).get("data", {}).get( "updatedResource", {}).get("data") logger.debug(callbackdatab64) try: callbackdata = base64.b64decode( callbackdatab64.encode("utf-8")) logger.debug(f"decoded successfully {callbackdata}") callback(callbackdata.decode("utf-8")) except Exception as e: logger.error(str(e)) response = self.client.execute(data=query, callback=secretcallback)
class RealTimeAwsSecretsMngr(): def __init__(self, **kargs): self.region = kargs.get("region") self.secretId = kargs.get("secretId") if (self.secretId == None or self.region == None): logger.error("secretId and region required") raise Exception("configuration error") self.client = AppSyncClient(**kargs) self.botoclient = boto3.client("secretsmanager", region_name=self.region) def subscribe(self, callback): response = self.botoclient.get_secret_value(SecretId=self.secretId) secretString = response.get("SecretString") callback(secretString) secretString = secretString.replace("\"", "\\\"") query = json.dumps({ "query": "subscription {\n updatedResource(id:\"arn:aws:secretsmanager:::" + self.secretId + "\") {\n id\n data\n }\n}\n" }) def secretcallback(client, userdata, msg): logger.debug("New data received : " + str(msg)) callbackdatab64 = json.loads(msg.payload).get("data", {}).get( "updatedResource", {}).get("data") logger.debug(callbackdatab64) try: callbackdata = base64.b64decode( callbackdatab64.encode("utf-8")) logger.debug(f"decoded successfully {callbackdata}") callback(callbackdata.decode("utf-8")) except Exception as e: logger.error(str(e)) response = self.client.execute(data=query, callback=secretcallback)
class RealTimeDynamoDB(): def __init__(self, **kargs): self.tableName = kargs.get("tableName") or config.getProperty( "tableName") self.primaryKey = kargs.get("primaryKey") or config.getProperty( "primaryKey") self.primaryKeyName = kargs.get( "primaryKeyName") or config.getProperty("primaryKeyName") self.sortKey = kargs.get("sortKey") or config.getProperty("sortKey") self.sortKeyName = kargs.get("sortKeyName") or config.getProperty( "sortKeyName") self.region = kargs.get("region") or config.getProperty("region") self.configColumnName = kargs.get( "configColumnName") or config.getProperty("configColumnName") self.tableAsConfig = kargs.get("tableAsConfig") or config.getProperty( "tableAsConfig") or "False" if self.tableAsConfig == "False": @DynamoDBTable(tableName=self.tableName, primaryKey=self.primaryKeyName, sortKey=self.sortKeyName, region=self.region) class DynamoTable(): pass self.table = DynamoTable() self.config = {} self.apiId = kargs.get("apiId") or config.getProperty("apiId") if (self.tableName == None or (self.tableAsConfig == "False" and (self.primaryKey == None or self.sortKey == None))): logger.error("mandatory fields missing") raise Exception("configuration error") self.client = AppSyncClient(region=self.region, apiId=self.apiId) def getValue(self): dynamoContent = self.table.get(self.primaryKey, scope=self.sortKey, column=self.configColumnName) return str(dynamoContent) def subscribe(self, callback): if self.tableAsConfig == "False": contents = self.getValue() callback(contents) id = "arn:aws:dynamodb:::" + self.tableName + ":" + self.primaryKey if self.sortKey != None: id = id + ":" + self.sortKey if self.tableAsConfig == "True": id = "arn:aws:dynamodb:::" + self.tableName query = json.dumps({ "query": "subscription {\n updatedResource(id:\"" + id + "\") {\n id\n data\n }\n}\n" }) def secretcallback(client, userdata, msg): logger.debug("New data received : " + str(msg)) callbackdatab64 = json.loads(msg.payload).get("data", {}).get( "updatedResource", {}).get("data") logger.debug(callbackdatab64) if (self.configColumnName != None): try: callbackdata = base64.b64decode(callbackdatab64.encode()) logger.debug(f"decoded successfully {callbackdata}") if self.tableAsConfig == "False": callbackdatacolumn = json.loads( callbackdata.decode()).get(self.configColumnName) else: callbackdatacolumn = json.loads(callbackdata.decode()) except Exception as e: logger.error(str(e)) callback(callbackdatacolumn) response = self.client.execute(data=query, callback=secretcallback)