How can i put a json body in a environnement variable [duplicate] - json

This question already has answers here:
Variable variable assignment error -"command not found"
(1 answer)
Difference between single and double quotes in Bash
(7 answers)
Closed 5 months ago.
I try to create a environnement variable containing this:
{
"type":"1234",
"project_id": "1234",
"private_key_id": "1234",
"private_key": "1234564789",
"client_email": "iam.gserviceaccount.com",
"client_id": "1072",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadatam"
}
I don't want to create the env variable "auth_url", "token_url" etc... I would like to create a variable that would contain all these elements.
I saw on the internet that we could do a $var = {"key", "index, ....}
but it doesn't work:
$GOOGLE_APPLICATION_CREDENTIALS={"type": "service_account", "project_id": "blabla"}
If someone has a solution.
Thanks for yours answers

Yes you can, but you need to put the whole value as a string, so the starting curly braces {} must be inside of the string as well. I mean you need to put whole json inside ' '.
GOOGLE_APPLICATION_CREDENTIALS='{"type": "service_account", "project_id": "blabla"}'
However, then in your app you will need to parse that string. For example, in node.js it would be const creds = JSON.parse(process.env.GOOGLE_APPLICATION_CREDENTIALS);
Or use corresponding function of the platform/language you use in your app.

Related

How to specify content type as application/json while sending message to azure service bus topic using an Azure Function? [duplicate]

This question already has answers here:
Azure Function - Python - ServiceBus Output Binding - Setting Custom Properties
(2 answers)
Closed 8 months ago.
I'm using an Azure Function (Python) to send a message to a Service Bus topic whenever a file lands in blob storage following a similar set up to that outlined here.
In particular, in order to send the message I have this in the JSON file:
{
"type": "serviceBus",
"direction": "out",
"connection": "AzureServiceBusConnectionString",
"name": "msg",
"queueName": "outqueue"
}
and in init.py file I have msg.set(input_msg) where input_msg is a JSON string, the output of doing json.dumps(list(reader)) on a CSV string.
When this message is picked up by the topic and subscriptions it has content type set to text/plain, whilst I'd like this to be application/json as mentioned here.
Is there a way to set this, for instance when I do msg.set, is there a way to specify the content type?
Full code:
init.py
def get_json_content_from_csv(csv_content: str) -> str:
reader = csv.DictReader(io.StringIO(csv_content))
json_content = json.dumps(list(reader))
return json_content
def main(event: func.EventGridEvent, msg: func.Out[str]):
data = event.get_json()
url = data["url"]
input_blob = BlobClient.from_blob_url(url, DefaultAzureCredential())
csv_content = input_blob.download_blob(encoding='UTF-8').readall()
json_content = get_json_content_from_csv(csv_content)
msg.set(json.dumps(json_content))
function.json
{
"bindings": [
{
"type": "eventGridTrigger",
"name": "event",
"direction": "in"
},
{
"type": "serviceBus",
"direction": "out",
"connection": "AzureServiceBus",
"name": "msg",
"topicName": "dev-iris-service-bus-topic"
}
]
}
According to this github issue for the Python SDK:
Cannot set Service Bus Message ContentType - Github Issue
The github issue response points to the docs here to set the contentType property on the message class
https://learn.microsoft.com/en-us/python/api/uamqp/uamqp.message.messageproperties?view=azure-python

Does config.GetSection not work in Azure Functions? And what is the recommended alternative?

Azure Function with a complex (List of objects) configuration type is working locally (with that complex type in local.settings.json) but fails to read / create list of objects in Azure (with that complex type in Azure Function configuration settings). I'm looking for the recommended / optimal way to support that across both platforms / methods of access.
This works great in my local.settings.json where I use the configuration builder and pull data out like
var myList = config.GetSection("ConfigurationList").Get<List<MyType>>();
however this doesn't seem to work in Azure Functions?? Now I think that is because in local.settings.json it is a json file and looks like
"ConfigurationList" : [ { "Name": "A", "Value": 2 }, { "Name": "B", "Value": 3 }]
while in Azure Functions it is a setting "ConfigurationList" with the value
[ { "Name": "A", "Value": 2 }, { "Name": "B", "Value": 3 }]
(so there isn't really a "section" in Azure Functions?)
It seems like the "easy" solution to this is to just change the .json to be a quoted string and deserialize the string (and then it would work the same in both places); but that doesn't seem like it would be the "best" (or "recommended" solution)
i.e. something like
"ConfigurationList" : "[ { \"Name\": \"A\", \"Value\": 2 }, { \"Name\": \"B\", \"Value\": 3 }]"
var myList = (List<MyType>)JsonConvert.DeserializeObject(config["ConfigurationList"], typeof(List<MyType>));
Which isn't the worst; but makes the json a bit "not as nice" and doesn't "flow" across the two platforms ... if it is what I have to do, fine; but hoping for a more standard approach / recommendation
As I metioned in the comment, on local you can process local.settings.json as a json file, but when on azure, the value in configuration settings is environment variable. There is no section, it just string.
Please notice that only string values are allowed, and that anything nested will break. Learn how to use nest settings on azure web app(azure functon is based on azure app service sandbox, so it is the same.):
https://learn.microsoft.com/en-us/archive/blogs/waws/asp-net-core-settings-for-azure-app-service
For example, if this is the json structure:
{
"Parent": {
"ChildOne": "C1 from secrets.json",
"ChildTwo": "C2 from secrets.json"
}
}
Then in web app, you should save it like this:
(source: windows.net)
Not sure if you are looking something like this , it seems a list but if it is a simple JObject like
"ConfigurationList" : {
"Name": "A",
"Value": 2
}
Then you can declare ConfigurationList:Name , ConfigurationList:Value in the configuration settings of function app

The name cannot contain any of the following symbols: '[, ], .'.'. for Compose in Azure Logic App

I have below JSON which I need to update inside a logic app
{
"name": "SampleDoc",
"type": "123",
"properties": {
"GP.Test": "M1",
"MG.Test": "C1"
}
}
I have used following setProperty syntax: -
#setProperty(variables('ResponseBody'),'properties', setProperty(variables('ResponseBody')['properties'], 'test','abc'),
setProperty(variables('ResponseBody')['properties'], 'GP.Test','M2'))
My desired JSON output should be
{
"name": "SampleDoc",
"type": "123",
"properties": {
"GP.Test": "M2",
"MG.Test": "C1"
}
}
But when I am running this, I am getting this error: -
InvalidTemplate. Unable to process template language expressions in action 'Compose' inputs at line '1' and column '2617': 'The provided property name 'GP.Test' has these invalid characters '.'. The name cannot contain any of the following symbols: '[, ], .'.'.
Could anyone suggest if we can handle '.' inside compose or any other way for achieving this?
Yes that is correct behavior that is occurring in the logic apps. The reason is that you have used the set property function to set the value of the GP.Test property. When working with the expressions in logic apps, the '.' operator is reserved operator and will be used to access sub properties etc of the expressions, functions etc. Hence you get the error. The solution to this is actually simple, you use the compose action directly without using the set property. Sample screenshot below.
Or if you want complex transformations, then using the liquid transformations through the integration account is the way to go

How to access environment variables in JSON file for Golang project?

My team is using Golang for coding, and we put all configurations in a conf.json, interesting part shown below, and another config_schema.json file for the json validation.
{
"host": "192.168.0.34",
"port": "5678",
"username": "test_user",
"password": "random_pass",
"dbName": "dummy"
}
My question is, can I define environment variables .e.g $USER and $PASS and use them as below or there is a proper way to achieve this for security purpose?
{
"host": "192.168.0.34",
"port": "5678",
"username": "$USER",
"password": "$PASS",
"dbName": "dummy"
}
Maybe it's too late since the question it's 1 year old
But you can use a combination of https://golang.org/pkg/os/#ExpandEnv
and https://golang.org/pkg/encoding/json/#Unmarshaler
In order to use Unmarshaler you should have a struct corresponding to your json. then you can override the default method and substitute only fields you want to. The simpler example is shown below
result := os.ExpandEnv(jsonString)
live example: https://play.golang.org/p/78C2zyYP6vL
We end up using the env var, and define username and password in Gitlab CI environment variables and enable masked so that the they will not display in the log.

Incorrect curl Post syntax for Elasticsearch in Postman

I'm a new learned and follow AWS example here: https://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-gsg-upload-data.html to build my first elasticsearch application.
I'm using Postman for curl command POST to append new document and index as shown here. However it seems the syntax is incorrect highlight by Postman so see if support to correct, thank you!
If you're posting to the _bulk URL, then the lines 1 through 6 should be on a single line without newlines. Same for lines 7 and 8. And you need to add a single newline at the end. Like this:
{ "index": {"_index": "movies", "_type": "movie", "_id": "2" }}
{ "director": "...", ...} + add a new line at the end of this line