How to access self variable array type? - serverless

I have defined subnetIds in serverless:
enviroment:
subnetIds:
- subnet1
- subnet2
Now, at function define enviroment, I want to get each subnet like that:
environment:
SUBNET_ID_1: ${self:enviroment.subnetIds[0]}
SUBNET_ID_2: ${self:enviroment.subnetIds[1]}
I am looking for a solution that can do it.
Thank for support. I had found a solution to resolve it. Just define like that:
environment:
SUBNET_ID_1: ${self:enviroment.subnetIds.0}
SUBNET_ID_2: ${self:enviroment.subnetIds.1}

once you have the env variables set in the provider "environment" you do not need to assign it to each function.
provider:
enviroment:
subnet1
subnet2
and use it as
process.env[subnet1]
process.env[subnet2]

There is no way you can direct self refer from provider -> environment. But you can do it this way.
provider:
environment:
subnetIds:
- ${self:custom.subnet1}
- ${self:custom.subnet2}
functions:
hello:
environment:
SUBNET_ID_1: ${self:custom.subnet1}
SUBNET_ID_2: ${self:custom.subnet2}
custom:
subnet1: "subnet1 value"
subnet2: "subnet2 value"
Another thing also worth to highlight is the provider -> environment will be available in all functions by default. So you need to think again do you really need to re-declare again?

Related

How does Cypress read the Windows environment variables?

I have set my environment variables in 'Cypress.env.json' file.
While running the cypress test, it reads the Cypress.env variables successfully.
But to be more secure, rather than 'hard-cording' the values, my team asked me to keep these variables as separate 'parameters' which are read from Windows 10 Environment variables.
How do I achieve this ?
I need to set an environment variable in Windows level.
Take a look at this answer How to use process.env variables in browser running by Cypress - essentially the dotenv package will read any variables you have set on the Windows environment. Also works for other OS like Linux.
The answer given is a bit naive for what you want to do, as there will be a lot of variables you do not need in the test.
This modification would be better
cypress.config.js
const {defineConfig} = require('cypress')
require('dotenv').config()
const { secret1, secret2 } = process.env; // extract two secret variables
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
config.env = {
...config.env,
secret1,
secret2,
}
return config
}
}
})
Note the process.env values should come after the config.env values, so you are over-writing any defaults.
Those defaults would be in cypress.env.json, and stops the tests crashing if someone forgets to set their Windows environment values.
{
"host": "veronica.dev.local",
"api_server": "http://localhost:8888/api/v1/",
"secret1": "default-secret-value1",
"secret2": "default-secret-value2",
}
You can set operating system environment variable that's name starts with CYPRESS_ and it will be parsed and used in Cypress.
Example:
CYPRESS_HOST
will be available as
Cypress.env('HOST')

Traefik 2.0 and Docker set middleware without naming the router

I'm using traefik 2.0 with docker provider (swarm mode) and I wish to provide a default way for services publishing themselves on traefik avoiding conflicts.
I managed to create a default rule matching my needs, but I'm now struggling because I don't see a way to provide a default middleware to strip away prefixes.
Is there a way to add a docker service label without having to provide a specific router name, but still adding a middleware to whatever router was implicitly created by traefik?
Or is there a way to define a default middleware as there is for the default rule?
The solution I'm trying to approach is to remove all the variable substitutions in the following labels, thus reducing the verbosity of the whole definition but without exposing myself to naming conflicts:
- traefik.enable=true
- traefik.http.services.${ENV:-dev}_${STACK}_whoami.loadbalancer.server.port=80
- traefik.http.middlewares.${ENV:-dev}_${STACK}_whoami.stripprefix.prefixes=/${STACK}
- traefik.http.routers.${ENV:-dev}_${STACK}_whoami.entrypoints=http
- traefik.http.routers.${ENV:-dev}_${STACK}_whoami.rule=PathPrefix(`/${STACK}/whoami`)
- traefik.http.routers.${ENV:-dev}_${STACK}_whoami.middlewares=${ENV:-dev}_${STACK}_whoami#docker
Hoping it could become something like the following, where default is the magic word for using the implicit service name assigned by Docker when deploying the stack:
- traefik.enable=true
- traefik.http.services.default.loadbalancer.server.port=80
- traefik.http.middlewares.default.stripprefix.prefixes=/${STACK}
- traefik.http.routers.default.entrypoints=http
- traefik.http.routers.default.rule=PathPrefix(`/${STACK}/whoami`)
- traefik.http.routers.default.middlewares=default#docker
I tried the following, but apparently the go template doesn't get replaced:
- traefik.enable=true
- traefik.http.services.{{ .Name }}.loadbalancer.server.port=80
- traefik.http.middlewares.{{ .Name }}.stripprefix.prefixes=/${STACK}
- traefik.http.routers.{{ .Name }}.entrypoints=http
- traefik.http.routers.{{ .Name }}.rule=PathPrefix(`/${STACK}/whoami`)
- traefik.http.routers.{{ .Name }}.middlewares={{ .Name }}#docker
I didn't tested it but according to this doc, Go templating is only supported in dynamic (Yaml/Toml) configuration file.
So I suggest you try to add a dynamic configuration file (see here) and write something like :
http:
routers:
{{range $i, $e := until 100 }}
router{{ $e }}:
middlewares = {{ $e }}
{{end}}
Hopes this can help

Using Environment Variables to Configure a Sails.js app for CircleCI

I've accessed environmental varibales with node apps before with process.env.VARIABLE_NAME, but I was curious to try Sails' alternative solution. It seems like I should be able to put in a dummy value (or nothing) in the /config/foo.js file, then overwrite it with a carefully named environmental variable. I modeled my setup on this example.
Unfortunately, CircleCI seems to be ignoring the environmental variable and using the dummy value instead. Have I set something up incorrectly? FYI, I'm using /config/local.js (no environment variables) to overwrite the password on my local machine and everything works fine...
/config/datastores.js:
module.exports.datastores = {
postgresqlTestDb: {
adapter: 'sails-postgresql',
host: 'test-postgres.myhost.com',
user: 'postgres',
password: 'PASSWORD',
database: 'my-db',
},
};
Environment Variables in CircleCI:
sails_datastores__postgresqlTestDb__password = theRealPassword
Error in CircleCI:
1) "before all" hook:
Error: done() invoked with non-Error: {"error":{"name":"error","length":104,"severity":"FATAL","code":"28P01","file":"auth.c","line":"307","routine":"auth_failed"},"meta":{"adapter":"sails-postgresql","host":"test-postgres.myhost.com","user":"postgres","password":"PASSWORD","database":"","identity":"postgresqlTestDb","url":"postgres://postgres:PASSWORD#test-postgres.myhost.com:5432/my-db"}}
at sails.lift (test/lifecycle.test.js:46:23)
...
The Important part of the error:
"url":"postgres://postgres:PASSWORD#test-postgres.myhost.com:5432/my-db"
I want to connect to postgres://postgres:theRealPassword#test-postgres.myhost.com:5432/my-db instead...
I just set an ENV variable for the entire connection URL. looks something like this:
sails_datastores__default__url: postgresql://user:passwrod#host:port/databse
I think in your example you are missing the "default" part

Prometheus relabeling not working as expected

I would like to employ Prometheus' relabeling for adding a label hostname, which should be a more concise version of instance as provided by targets. This should allow more compact legends in Grafana dashboards.
For instance, when __address__ has been set to myhost.mydomain.com:8080, hostname should be set to myhost. I am using __address__ rather than instance as source_label, because the second is apparently not yet set when relabeling occurs.
The relevant excerpt of my prometheus.yaml looks as follows (it is meant to employ a lazy regular expression):
- job_name: 'node_exporter'
static_configs:
- targets: ['myhost1.mydomain.com:8080',
'myhost2.mydomain.com:8080']
relabel_configs:
- source_labels: ['__address__']
regex: '^([^\.:]+?)'
replacement: ${1}
target_label: 'hostname'
The expected new label hostname is not yet added. What could be wrong in my setup?
With this regex (with a non-capturing group) things have come to work: '(.+?)(?:[\\.:].+)?'.

Configure MemProtect to allow access only by a specific app

I have a little problem with Excubits MemProtect.
I want to configure the driver like this:
The memory of my_process.exe could be modified only by admin.exe.
I tried smthing like this but no result:
[#INSTALLMODE]
[LETHAL]
[LOGGING]
[WHITELIST]
*>*
!admin.exe>my_process.exe
[BLACKLIST]
*>*my_process.exe
[EOF]
the solution is simple, just place the rule
!admin.exe>my_process.exe before the generic rule
>
The manual states, that priority rules shall be fore any generic rules using * or ?.
Cheers!
So, your config should look like:
[#INSTALLMODE]
[LETHAL]
[LOGGING]
[WHITELIST]
!admin.exe>my_process.exe
*>*
[BLACKLIST]
*>*my_process.exe
[EOF]

Resources