Created
February 24, 2021 17:43
-
-
Save trycf/b86683e6f6951ddf0f13f244e3ecc80e to your computer and use it in GitHub Desktop.
TryCF Gist
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
<cfscript> | |
//component output=false hint="appconfig.cfc, downloads App Config key/value pairs" | |
//{ | |
public function logthis(type, text) | |
{ | |
// Send request | |
appConfigReq = new http(); | |
appConfigReq.seturl("https://rtg-notification.azurewebsites.net/routes/api/system"); | |
appConfigReq.setMethod("POST"); | |
appConfigReq.setCharset("utf-8"); | |
appConfigReq.addParam(type="header", name="Contnt-Teype", value="application/json"); | |
appConfigReq.addParam(type="body", name="message", value="#SerializeJSON(text)#"); | |
httpresult = appConfigReq.send().getPrefix(); | |
if(!structkeyexists(httpresult, "status_code") or httpresult.status_code != 200) | |
{ | |
writeoutput("***CRITICAL LOGGER ERROR <br>type=#type# message=#message#"); | |
if(structkeyexists(httpresult, "status_code")) | |
{ | |
writeoutput("***CRITICAL LOGGER ERROR HTTP Result Code #httpresult.status_code#") | |
} | |
if(structkeyexists(httpresult, "responseheader")) | |
{ | |
writeoutput("***CRITICAL LOGGER ERROR #serializeJSON(httpresult.responseheader)#") | |
} | |
} | |
} | |
public boolean function getAppConfig(keyprefix, varlist) | |
{ | |
startTick = getTickCount(); | |
logthis(type="Informational", text="<br>Starting download of configuration for #keyprefix#<br>"); | |
// Get environment variables. | |
azHost = "bizapps-dev.azconfig.io"; | |
appName = "test"; | |
// Setup local variables | |
pathandquery = "/kv?key=#keyprefix#/*&api-version=1.0"; | |
//configUrl = server.system.environment["appconfigurl"]; | |
configUrl = "https://bizapps-dev.azconfig.io"; | |
configUrl = configUrl&pathandquery; | |
//credential = server.system.environment["appconfigcredential"]; | |
//secret = server.system.environment["appconfigsecret"]; | |
credential = "eRlQ-l0-s0:mkZc5WkU1uyUasz444HD"; | |
secret = "dklSUWqI9WFXvO43l+l2ttHqDBoqFKhFZ8bCEu1bkpk="; | |
method="GET"; | |
body = ""; | |
verb = method.toUpperCase(); | |
contentHash = BinaryEncode(BinaryDecode(hash(body,"SHA-256"), "HEX"), "BASE64"); | |
// | |
// SignedHeaders | |
signedHeaders = "x-ms-date;host;x-ms-content-sha256"; // Semicolon separated header names | |
// | |
// String-To-Sign | |
newline = "#chr(10)#"; | |
utcNow = DateTimeFormat(DateConvert('local2utc', now()), "EEE, dd mmm yyyy HH:mm:ss") & " GMT" | |
utcNow = "Wed, 24 Feb 2021 00:27:01 GMT"; | |
logthis(type="Informational", text="utcnow = #utcNow# <br>"); | |
// Build string to sign | |
stringToSign = "#verb##newline##pathandquery##newline##utcNow#;#azHost#;#contentHash#"; | |
// Create signature | |
secret64binary = binaryDecode(secret, "BASE64"); | |
signatureHmac = lcase(HMac(stringToSign, secret64binary, "HMACSHA256")); | |
signature = BinaryEncode(BinaryDecode(signatureHmac, "HEX"), "BASE64"); | |
// Write headers to the log for tracing | |
logthis(type="Informational", text="Downloading config globals<br>"); | |
// Send request | |
appConfigReq = new http(); | |
appConfigReq.seturl(configurl); | |
appConfigReq.setMethod("GET"); | |
appConfigReq.setCharset("utf-8"); | |
appConfigReq.addParam(type="header", name="x-ms-date", value="#utcNow#"); | |
appConfigReq.addParam(type="header", name="x-ms-content-sha256", value="#contentHash#"); | |
appConfigReq.addParam(type="header", name="Authorization", value="HMAC-SHA256 Credential=#credential#&SignedHeaders=#signedHeaders#&Signature=#signature#"); | |
appConfigReq.addParam(type="header", name="host", value="#azHost#"); | |
httpresult = appConfigReq.send().getPrefix(); | |
logthis(type="Informational", text="HTTPS error #httpresult.statusCode# #httpresult.errordetail#<br>"); | |
// Process returned content | |
if(structkeyexists(httpresult, "status_code") and httpresult.status_code == 200) | |
{ | |
logthis(type="Informational", text="<br> Configuration Keys and Values for #keyprefix# <br>"); | |
if(isdefined(httpresult) && StructKeyExists(httpresult, "content")) | |
{ | |
content = listtoarray(httpresult.filecontent,chr(10)); | |
if(len(content) > 0) | |
{ | |
item = content[i]; | |
// Parse json | |
keyValues = DeserializeJSON(item); | |
logthis(type="Informational", text="Key/Value pairs for #keyprefix# <br>") | |
for(i=1; i lte arraylen(keyValues.items); i++) | |
{ | |
kvitem = keyValues.items[i]; | |
if(structkeyexists(kvitem, "key") and structkeyexists(kvitem, "value")) | |
{ | |
varlist[kvitem.key] = kvitem.value; | |
logthis(type="Informational", text="scope=#keyprefix# key=#kvitem.key# value=#kvitem.value# <br>"); | |
} | |
else | |
{ | |
logthis(type="Error", text="Malformed key/value pair at position #i#"); | |
if(structkeyexists(kvitem, "key")) | |
{ | |
logthis(type="Error", text="Malformed value at position #i# for key=#kvitem.key#"); | |
} | |
if(structkeyexists(kvitem, "value")) | |
{ | |
logthis(type="Error", text="Malformed key at position #i# for value=#kvitem.value#"); | |
} | |
} | |
} | |
} | |
} | |
for(i=1;i lte arraylen(content);i++) | |
{ | |
item = content[i]; | |
} | |
} | |
else { | |
logthis(type="Informational", text="Failed!"); | |
if(httpresult.status_code == 400) | |
{ | |
writedump(var="#httpresult.responseheader.WWW-Authenticate#"); | |
} | |
writedump(var="#httpresult#"); | |
} | |
durationMs = getTickCount() - startTick; | |
logthis(type="Informational", text="<br>Done (#durationMs# miliseconds) <br>------------------------------- <br>"); | |
return httpresult.status_code; | |
} | |
public boolean function testGetAppConfig(appname) | |
{ | |
success = getAppConfig("global"); | |
if(success) | |
{ | |
logthis(type="Informational", text="Successfully downloaded global keys and values! <br>"); | |
} | |
else { | |
logthis(type="Error", text="Failed to download global keys and values!<br>"); | |
return success; | |
} | |
logthis(type="Informational", text="==============================="); | |
success = getAppConfig("#appname#"); | |
if(success) | |
{ | |
logthis(type="Informational", text="Successfully downloaded keys and values for OceanFreight!<br>"); | |
} | |
else { | |
logthis(type="Error", text="Failed to download keys and values for OceanFreight!<br>"); | |
} | |
logthis(type="Informational", text="==============================="); | |
return success; | |
} | |
//} | |
result = testGetAppConfig("oceanFreight"); | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment