Display all commit messages since the last tag:
|
|
I have difficulties using the Vault dev server mode in docker-compose environment. Other people have difficulties too.
Partly it’s due to incomplete/imperfect Vault documentation. I had to skim through the source code to understand what parameters can be used for some commands and for the dev server itself. Some of these were hard to find in the documentation.
I want Vault running in compose env with these features enabled on start:
key1 and key2Here is a solution that works well.
Just use this. If you want to read some details follow below.
docker-compose.yml
|
|
vault-init.sh
|
|
That’s it.
The dev server is always unsealed by default, so our first objective is achieved.
Hashicorp Vault Transit Engine API allows signing of data and verification of signatures. It offers more features like encryption/decryption, hashing, random bytes generation, but in this post we’re concerned only with the signing part of the API and more specifically with converting public keys returned by the Vault API to JWK format.
Example response when fetching a public key from Vault:
|
|
Some keys like ED25519 are just random bytes and are returned as base64.StdEncoding string, while other key types
like ECDSA and RSA have some structure and are returned as PEM encoded keys.
Below is an example function that creates a jose.JSONWebKey from a Vault public key.
|
|
Example JSON encoding of a converted JWK ECDSA-P256 public key would look like:
git reflog is underrated.
I sometimes see people who use private Gitlab subgroups to name their Go
modules (mostly libraries) with a suffix .git
Let’s see an example. If we have a Go module project at the following imaginary location:
|
|
Now when running go get gitlab.com/myuser/foogroup/barlib from another Go project,
even though the SSH keys are set correctly, we may get an error:
|
|
We start googling and reading stackoverflow and it seems that a possible solution is to rename
the module to gitlab.com/myuser/foogroup/barlib.git … and the issue looks fixed.
Also at this point people start to doubt the goodness of the Go tools
(go mod and go get especially) and mark this as a stupid annoying (Go) bug.
The solution is simple and the reason for the issue too and it’s not a bug.
Why this happens is that go get tries to discover the modules at a given path
in order to find the requested Go module repository. Only after the repository is found,
the tools will do git clone or git checkout and the SSH keys will be used for authentication.
The issue comes down to the fact that private Gitlab subgroups cannot be listed/viewed
without a Gitlab Access Token.
Go to Gitlab->Preferences->Access Tokens and create one. Then create (or edit) a .netrc
file in your home folder: ~/.netrc The contents of the file are below:
And we may not need to name our Go modules with a .git suffix anymore.
In some situations it’s not obvious how to use the RETURNING SQL clause with Gorm and databases that support it, like CockroachDB.
The particular use case where I found it challenging is the following: I want to update multiple records with a WHERE clause, ORDER BY clause and LIMIT clause and immediately return the updated records without a second query.
The simplified CockroachDB table schema without indices and noise is:
My initial attempts were as follows.
|
|
Gorm builds the SQL statement successfully, but for some reason doesn’t include the ORDER BY and LIMIT clauses, which are very important in my particular case. Ok, let’s try with a raw SQL statement.
|
|
Gorm executes the complete correct query, but I couldn’t find a way to return/populate the records slice - it was always empty (tried with different arrangements and examples from the docs). Without any deep investigation on why that happens, it seems that Gorm just ignores the RETURNING data when executing raw SQL statements.
What finally worked for me seems a bit like a hack, but it does the job. By using the ORDER BY and LIMIT clauses directly in the WHERE clause, the query works correctly as I need it.
|
|
The examples use Updates with
map[string]interface{}but the issue described here is not affected by that. Using struct or other ways for updating produces the same results.
LIMIT when using a RETURNING clause so another solution could be used, like SELECT FOR UPDATE -> then update, and return the updated record.A simple Go implementation for graceful shutdown of HTTP servers with Go 1.8+ wrapped in ~20 lines of code.
|
|
The graceful shutdown function can also be used directly as a package from github.com: Graceful Shutdown
You should use Go 1.8+
The function doesn’t support TLS servers currently as I don’t need it, but it should be trivial to add that.
|
|
vendor in our Go projects?Since the introduction of Go modules, the prevalent opinion on the internet is that we should not commit the vendor directory of our projects.
The argument against committing vendor is that it’s unnecessary, because the Go tools can resolve and find dependencies when needed on-the-fly. All public Go modules are also hosted by various Go Proxy servers for redundancy.
While I agree that it’s technically
unnecessary and bloats the repo, I’d like to give some
arguments in favour of committing vendor.
Building the project doesn’t depend on some code being available on Github/Gitlab/… or the Go Proxy servers. Open source projects may disappear because of censorship, authors incentives, licensing changes or some other reasons I can’t currently think of. Not in your repo, not your code.
We may use internal or 3rd party Go modules (private) which may also disappear or become inaccessible, but if they are committed in vendor, they are part of our project and nothing breaks unexpectedly.
Go modules may not follow semantic versioning, which means the Go tools will rely on the latest commit hash when fetching them on-the-fly. Repo history may be rewritten (e.g. rebase) and you, a colleague or your CI job may end up with different code for the dependencies they use.
CI/CD jobs which perform compilation and build steps need
not waste time and network to download the dependencies
every time the CI job is executed. All needed dependencies
are local and present (go build -mod vendor)
CI/CD jobs which perform compilation and build steps may not be configured additionally to authenticate against private repos, so that they can download the dependencies.
Committing vendor can sometimes improve the code review process. Typically we’re committing dependency changes in a separate commit, so they can be easily viewed if the reviewer is curious.
Here’s an interesting observation related to bloating the repo. If I make code review, and a team member has included a new dependency with 300 files (or updated one with 300 files changed), I may be curious to deep dive into that and start a discussion about the code quality, the need for this change or alternative Go modules. This may lead us to actually decrease our binary size and overall complexity.
If I just see a single new line in go.mod in new MR, chances are I won’t even think about it.
For my own peace of mind I would always prefer to commit the vendor directory alongside go.mod and go.sum while at the same time try to keep the project dependencies at minimum.
vendor directory SHOULD NOT be committed for Go
libraries, frameworks or code which doesn’t produce
executable binaries, unless you have a really good
reason to do it.If we want to preserve (pass) the command line arguments when running a bash script and pass them to a program defined in the script, we can use the $* pattern after the call. An example bash script called myscript.sh is shown below. It sets some environment variables (optional) and starts a program called myserver. The program will accept all command line args passed to the bash script as if they were passed to the myserver program directly.
Calling the script like:
|
|
will be like calling the myserver program like:
|
|