Skip to content

Replace md5 with sha256 algorithm - #9021

Merged
taylorsilva merged 1 commit into
concourse:masterfrom
IvanChalukov:master
Jan 23, 2025
Merged

taylorsilva merged 1 commit into
concourse:masterfrom
IvanChalukov:master

Conversation

@IvanChalukov

Copy link
Copy Markdown
Contributor

Changes proposed by this PR

closes #9020

Switching md5 algorithm to sha256 across the code base and add migration for database naming convention.

  • done

Notes to reviewer

Tested on FIPS compliant database and on already existing database.

Release Note

  • Switches md5 algorithm to sha256 across the code base
  • Renames database related entries from md5 to sha256 suffix

@IvanChalukov
IvanChalukov requested a review from a team November 1, 2024 13:47

@Kump3r Kump3r left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tested something really similar, due to my closed issue #9007. I tried with FIPS and non FIPS compliant databases, both worked like a charm locally and on my bosh environment. When having multiple webs in rolling update, I can see some temporary trouble, due to the first web migrating the data and the second still working with md5 columns, but that is expected with such migrations. The other way would be to not rename the columns at all, since they are used for UID from what I can gather. But that wouldn’t be okay imho. Looks good to me, good job!

@Kump3r

Kump3r commented Nov 4, 2024

Copy link
Copy Markdown
Contributor

The validations are currently failing due to check comment. Once the issue is fixed in upstream, or the module is reverted, we will recheck.

@Kump3r

Kump3r commented Nov 5, 2024 •

Copy link
Copy Markdown
Contributor

@taylorsilva since the PR resource uses the broken base at the moment still and I don't have permissions to recreate the version, what is the procedure, so we can trigger the PR validations with the correct base? Thanks!

Edit: clear resource cache seem to do the trick I guess

@taylorsilva

Copy link
Copy Markdown
Member

@Kump3r it's fixed now, so you should be unblocked

@Kump3r

Kump3r commented Nov 5, 2024 •

Copy link
Copy Markdown
Contributor

Yes, just the failing PR's with this error need to clear the cache of the resource, since otherwise it will not fetch the new base. Btw I am not sure if bug is the correct label for such a PR, feel free to adjust as necessary 😅

@taylorsilva

Copy link
Copy Markdown
Member

Haven't had a chance to review this. Happy to see all the test passed though! I think this should go into a minor release version since it's a big change. Will definitely take this in once I have time to review it.

@taylorsilva taylorsilva added this to the v7.13.0 milestone Dec 4, 2024
@taylorsilva

Copy link
Copy Markdown
Member

Just making a note: one concern I have is how long will the migration take on a large Concourse database? I'm reminded of #8499 which takes 30mins on a large database.

I sadly don't have access to a large database. I plan to create one and do some manual acceptance testing.

@Kump3r

Kump3r commented Jan 4, 2025

Copy link
Copy Markdown
Contributor

From my side I haven’t tested with a heavily loaded database, but if I manage to allocate some time, I can try it out and let you know as well. Thanks for the remark, it is a valid concern.

@IvanChalukov

Copy link
Copy Markdown
Contributor Author

I unfortunately haven't had the opportunity to test with a heavily loaded database either, but I'll make sure to give it a try as soon as I have the chance. Thanks for pointing that out!

@taylorsilva

Copy link
Copy Markdown
Member

Would be a big help if either of you can do that!

@Kump3r

Kump3r commented Jan 14, 2025 •

Copy link
Copy Markdown
Contributor

I will test it just to be sure when I allocate some free time, but in the meantime it is worth mentioning that the migration you provided, migrates data to bigint, whereas here, we do not migrate the data, as the values are used as UID’s in the code, so we only need to rename the columns and indexes. If it wasn’t a bad practise, we wouldn’t even need a migration, because we could keep the columns named md5 etc, and just switch the encryption for newly created UID’s. Just my two cents on the topic. Anyway it doesn’t seem that hard to populate a bunch of data in the columns with Concourse and try switching the algorithm to sha256, to check how stable the migration is.

@IvanChalukov

Copy link
Copy Markdown
Contributor Author

Together with @Kump3r manage to setup database and loaded up with 76Gb out of 100Gb (at 76%). The data was spread across 12 teams and ~600 pipelines(each team contains 50 pipelines). Each pipeline consists of a single job that constantly emits large data (output of dmesg - 290 times).
We've run the same query for all of the tables:

SELECT COUNT(version_md5) AS row_count FROM <table_name>;

Results:

Table name number of entries
resource_config_versions 15725
build_resource_config_version_inputs 36969
build_resource_config_version_outputs 450
next_build_inputs 1690
resource_caches 51
resource_disabled_versions 0

Migration was executed for the same duration as an empty Concourse database. No load spikes, only expected downtime mentioned earlier.

Based on @taylorsilva recommendations, we're setting up a new instance that will have more than a million entries in builds table. We will provide feedback as soon as migration is completed.

@Kump3r

Kump3r commented Jan 23, 2025 •

Copy link
Copy Markdown
Contributor

Based on the previous comment and @taylorsilva's recommendations, we tested the migration together with @IvanChalukov.

The parameters of the test were as follows:

  • A database instance containing over 1mil.builds:
    atc=> SELECT COUNT(*) from builds;
      count
    ---------
     1705659
    
  • We've run the same query for all of the tables:
    SELECT COUNT(version_md5) AS row_count FROM <table_name>;
    Table name number of entries
    resource_config_versions 1706209
    build_resource_config_version_inputs 1728245
    build_resource_config_version_outputs 450
    next_build_inputs 2140
    resource_caches 1068
    resource_disabled_versions 0

Result:

The migration took the same duration as an empty Concourse database, which is around ~10s.. The whole web process started in around ~15s. with bosh deployment/virtual machines.

Conclusion:

Since this specific migration, does not migrate the data from the columns and only changes the names of the specific columns + indexes, the time for a migration, according to my understanding, doesn't depend on the amount of data in the affected tables.

Feel free reach out with additional scenarios or acceptance testing that needs to be done in order to complete the review process for this enhancement.

@taylorsilva

Copy link
Copy Markdown
Member

Thanks for the testing guys! I'm going to do some smaller local tests today and then I'll merge this later today or tomorrow.

@taylorsilva taylorsilva left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - Thanks for the PR guys! Looking forward to more PR's like this :)

@taylorsilva
taylorsilva merged commit a196d52 into concourse:master Jan 23, 2025
@aliculPix4D

aliculPix4D commented Mar 14, 2025 •

Copy link
Copy Markdown
Contributor

@taylorsilva

Yesterday, we created our own un-official internal release (v7.13.0-pix4d) from master branch (755216b) and deployed it to production.

Immediately after deployment, we noticed a spike of builds, and all builds were triggered because ALL resources found new versions even though the version didn't actually change...

e.g the last two versions for the git resource was reporting the same commit or the last two versions of timer resource reported exactly the same time version.

See the screenshot:

Screenshot from 2025-03-13 15-59-47

The only difference is that version_sha256 field in the database... e.g see the most recent versions for a git resource:

id     |   version   | version_sha256
8739718047 | {"ref": "f8e05fe3c22b81d2a1c57ba2a137833adcaf5cc9"} | ce430406369a472e7a92c4c23240fec7
8793637924 | {"ref": "f8e05fe3c22b81d2a1c57ba2a137833adcaf5cc9"} | 0b7b6fe5cb08b93bd11116e9705956c777b56b61456703ecafb0a621201918f4

Maybe, I am wrong, but it looks to me this occurs due to the changes in this PR.

Was this behavior expected? Is there a way to avoid it? If yes, I wanted to give you folks a heads-up that this can occur.

Sorry for writing here, instead of opening a new issue, but I wasn't sure if opening a bug report issue is the correct way forward for the unreleased Concourse version.

@taylorsilva

Copy link
Copy Markdown
Member

Thanks for reporting this @aliculPix4D

This is not ideal. I don't want an upgrade to trigger a bunch of jobs. That's VERY disruptive, especially for larger instances. I'm going to consider this a blocker for releasing.

Let me try and reproduce this on a local scale first.

Started up Concourse using 7.12.1 using docker compose.
Set this pipeline:

resources:
- name: some-resource
  type: time

jobs:
- name: some-passing-job
  plan:
  - get: some-resource
    trigger: true

Which will default to creating one version per calendar day. It does and triggers the job.

Next I'll upgrade to the latest on master and.... yup, it remade the same version and created a triggered a second build ☹️
image

Hmmm what can we do here?
Could we try converting all the SHA256's to MD5 in the db? My gut says that's not feasible as it will probably be too many records on old/large instances. The migration will likely take a long time if we do that.

Different approach might be to do some checks to see if a resource version already exists with a sha256? Might need to do that in a couple places in the atc/db package.

Could we update this to check for md5 or sha256? I have a feeling that would break FIPS compliance though, the whole motivation for this PR:

func (r *resourceConfigScope) FindVersion(v atc.Version) (ResourceConfigVersion, bool, error) {
rcv := &resourceConfigVersion{
conn: r.conn,
}
versionByte, err := json.Marshal(v)
if err != nil {
return nil, false, err
}
row := resourceConfigVersionQuery.
Where(sq.Eq{
"v.resource_config_scope_id": r.id,
}).
Where(sq.Expr("v.version_sha256 = encode(digest(?, 'sha256'), 'hex')", versionByte)).
RunWith(r.conn).
QueryRow()
err = scanResourceConfigVersion(rcv, row)
if err != nil {
if err == sql.ErrNoRows {
return nil, false, nil
}
return nil, false, err
}
return rcv, true, nil
}

Or maybe just avoid saving the new version at all:

func saveResourceVersion(tx Tx, rcsID int, version atc.Version, metadata ResourceConfigMetadataFields, spanContext SpanContext) (bool, error) {
versionJSON, err := json.Marshal(version)
if err != nil {
return false, err
}
metadataJSON, err := json.Marshal(metadata)
if err != nil {
return false, err
}
spanContextJSON, err := json.Marshal(spanContext)
if err != nil {
return false, err
}
var checkOrder int
err = tx.QueryRow(`
INSERT INTO resource_config_versions (resource_config_scope_id, version, version_sha256, metadata, span_context)
SELECT $1, $2, encode(digest($3, 'sha256'), 'hex'), $4, $5
ON CONFLICT (resource_config_scope_id, version_sha256)
DO UPDATE SET metadata = COALESCE(NULLIF(excluded.metadata, 'null'::jsonb), resource_config_versions.metadata)
RETURNING check_order
`, rcsID, string(versionJSON), string(versionJSON), string(metadataJSON), string(spanContextJSON)).Scan(&checkOrder)
if err != nil {
return false, err
}
return checkOrder == 0, nil
}

I think the tricky part here is not using Postgres's encode with md5 at all, because I believe that's what causes an app to not be FIPS compliant anymore. That's just a guess though as I really don't know anything about FIPS.

@IvanChalukov Would you have time to investigate this and maybe PR a solution?

@taylorsilva

Copy link
Copy Markdown
Member

Made an issue for further discussion and work reference: #9122

taylorsilva added a commit that referenced this pull request Mar 17, 2025
This reverts commit a196d52, reversing
changes made to 45bcaf4.
taylorsilva added a commit that referenced this pull request Mar 17, 2025
This reverts commit a196d52, reversing
changes made to 45bcaf4.

Signed-off-by: Taylor Silva <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

pq issue when using FIPS compliant OpenSSL library

4 participants