Hash a string in Declarative Pipeline - jenkins-pipeline

In a script inside a Declarative Pipeline (Groovy 2.4.11) I want to hash a string (preferably SHA-256).
Does anyone know a way of doing this without needing a signature approval?

The Pipeline Utility Steps plugin comes with a sha1 step.
Unfortunately, other SHA variants seem to be not available by community plugins.
EDIT: as of now (2021), also sha256 is available.

Had the same issue.
For me, using the Java approach is working (if you do not need the sandbox, or are willing to white-list the used methods):
import java.security.MessageDigest
#NonCPS
String getDigest(String input, String algorithm) {
MessageDigest digest = MessageDigest.getInstance(algorithm)
digest.update(input.bytes)
digest.digest().encodeHex().toString()
}
echo getDigest('foo', 'SHA256')
We'll use this method from within a Jenkins shared library defined globally in the Jenkins configuration, so it will be able to run outside the sandbox. Actually, we use the MD5 hash - but other algorithms are supported, too.
For a list of possible digest algorithms, you may check the official documentation for MessageDigest, which at the time of writing can be found here:
https://docs.oracle.com/javase/7/docs/api/java/security/MessageDigest.html

Related

Using dictionary rather than parameter.yml for Kedro

Is there a way to use dictionary rather than using a yaml config for parameters.yml? I want to keep it as a Python Object because my IDE can then track the dependency easily. For my parameters, I am injecting functions in it.
If i need to use yml, I will have to use
def steps1(x, func1):
func1 = eval(func1)
And this will break the refactoring features easily.
You could overwrite the params property in your src.package_name.run.ProjectContext so that it uses a Python dictionary instead of the config loader. You’re also welcome to write up your custom ConfigLoader and use that instead (by overriding _create_config_loader), but that’s probably more effort.
Please bear in mind that parameters in Kedro are meant to be “as dumb as possible” though, as it’s considered static configuration and it’s better separated out of code. What you describe, with expressions, sounds more suited for nodes.

Why can I not use aws_lambda_function datasource inside aws_lambda_alias routing_config?

I am experimenting with a blue/green deployment setup for lambdas using terraform and lambda aliases.
I am trying to automatically retrieve the previously deployed version of the lambda by using the aws_lambda_function data source and using the value inside the routing_config => additional_version_weights. This would allow me to set up a traffic split between the previously deployed version and the version that has just been deployed.
However, I have run into 2 errors I don't quite understand.
The first error is when I try and use the data source in conjunction with a regular variable. In this case terraform complains about being unable to parse the value.
If I hard code the value terraform will attempt to run the update, however, it will fail as it tries to set the version in the routing configuration to an empty value which causes a validation error. If I instead output the value I can see that the correct version is retrieved.
Example code and steps to reproduce can be found on link below.
https://github.com/jaknor/terraform-lambda-data-source-issue
Is anyone able to explain why this isn't working?
Please note, while I appreciate that there are other ways of achieving my goal, at the moment I am only interested in understanding these particular errors.
In Terraform v0.11 and prior, interpolation sequences are not supported on the left side of an = symbol introducing an argument or object key.
To generate a map with dynamic keys, you must instead use the map function:
additional_version_weights = "${map(data.aws_lambda_function.existing_lambda_func.version, var.lambda_previous_version_percentage)}"
In Terraform v0.12 (which is in beta as I write this) the parser is now able to distinguish between arguments (which must be constants in the configuration) and map keys (which can be arbitrary expressions) and so the following syntax is preferable, although the above will still work for backward compatibility.
additional_version_weights = {
(data.aws_lambda_function.existing_lambda_func.version) = var.lambda_previous_version_percentage
}
The additional parentheses around the key expression are important to tell Terraform that this should be understood as a normal expression rather than as a literal name.

Retrieve hostname -f within YAML(yml)

I've got a config .cfg file that has the hostname hard coded in it. I'm trying to find a way for the hostname to be gotten locally (dynamically) by running a command similar to hostname -f to have it configure the variable in the .cfg, without running a script, like python, to write the config file ahead time. Is it possible to run a 'yum' command that gets the hostname to use in the YAML/yml file?
Thanks to Wikipedia, I think I found out why no one is helping me with this:
Wiki YAML -> Security
Security
YAML is purely a data representation language and thus has no executable commands. While validation and safe parsing is inherently possible in any data language, implementation is such a notorious pitfall that YAML's lack of an associated command language may be a relative security benefit.
However, YAML allows language-specific tags so that arbitrary local objects can be created by a parser that supports those tags. Any YAML parser that allows sophisticated object instantiation to be executed opens the potential for an injection attack. Perl parsers that allow loading of objects of arbitrary class create so-called "blessed" values. Using these values may trigger unexpected behavior, e.g. if the class uses overloaded operators. This may lead to execution of arbitrary Perl code.
The situation is similar for Python or Ruby parsers. According to the PyYAML documentation

How to get annotation of go language function?

How to get annotation of go language function?
Example:
// #annotation1
// #annotation2
func Tags() string {
return ""
}
How to get the "#annotation1" and "#annotation2"?
Short answer, there is no native support for annotations in Golang. What's being used if tags, which you can get from the reflect package.
So, you do not have annotations in Go, and to my knowledge there is no library which provides them. Depending of what you want to do, usually tags are more than enough, and you can use the language's power to achieve the desired results.
It should be possible to implement them as you can get the documentation strings, just like PHP does. However, in the big majority of cases it won't be necessary.
EDIT:
In Go, you have access to the documentation of structs, fields, methods, interfaces, functions (godoc isn't magical) through the ast package. However, it requires parsing the files, there is no function such as type.getDocComments() as in PHP.
So, an implementation is theoretically possible. However, the kind of annotations you're asking for are simply not part of Golang's philosophy. There are plenty of libraries that extensively use tags, but none use annotations.
I do not know of any native support for something that pulls specific tags from comments - however, the builtin functionality of godoc does pull from the comments directly adjacent to functions. If you are trying to build documentation, this may be useful.
In addition to godoc, I know that the golang plugin for IntelliJ pulls these comments as the help/documentation for inline completions and suggestions.
Hope this helps!

Is there a SOAP::Lite equivalent library in ruby?

I want to build a SOAP client using ruby. I tried using the soap4r library to generate ruby classes out of the WSDL file, but the issue with this was that all the methods it generated were of optional kind, instead of NAME/VALUE pairs. Given that some methods have a very large number of arguments, many of which are optional, I would prefer to use something like SOAP::Lite(Perl Library) which does not depend on WSDL file and accepts arguments as NAME/VALUE pairs.
Also take a look at Savon.
I've not actually used this myself, but I remembered hearing about it the other day: Handsoap. Check it out and see if it fits your needs! ;)

Resources