-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] [BREAKING] [OS] previous versions of object store incorrectly b…
…ase64 encoded without padding - this made javascript created entries be incompatible with other clients. The tool bin/fix-os.ts can be used to fix existing object stores - however it is important for a backup of the stream to be made prior to using it.
- Loading branch information
Showing
4 changed files
with
210 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
/* | ||
* Copyright 2023 The NATS Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { parse } from "https://deno.land/std@0.177.0/flags/mod.ts"; | ||
import { ObjectStoreImpl } from "https://raw.githubusercontent.com/nats-io/nats.deno/main/nats-base-client/objectstore.ts"; | ||
import { | ||
connect, | ||
ConnectionOptions, | ||
credsAuthenticator, | ||
} from "https://raw.githubusercontent.com/nats-io/nats.deno/main/src/mod.ts"; | ||
|
||
const argv = parse( | ||
Deno.args, | ||
{ | ||
alias: { | ||
"s": ["server"], | ||
"f": ["creds"], | ||
"b": ["bucket"], | ||
}, | ||
default: { | ||
s: "127.0.0.1:4222", | ||
c: 1, | ||
i: 0, | ||
}, | ||
boolean: ["dryrun"], | ||
string: ["server", "creds", "bucket"], | ||
}, | ||
); | ||
|
||
const copts = { servers: argv.s } as ConnectionOptions; | ||
|
||
if (argv.h || argv.help) { | ||
console.log( | ||
"Usage: fix-os [-s server] [--creds=/path/file.creds] [--check] --bucket=name", | ||
); | ||
console.log( | ||
"\nThis tool fixes metadata subject in an object store that were written", | ||
); | ||
console.log( | ||
"with base64 encoding without padding.", | ||
); | ||
console.log("IMPORTANT - backup the object store before using this tool."); | ||
|
||
Deno.exit(1); | ||
} | ||
|
||
if (argv.creds) { | ||
const f = await Deno.open(argv.creds, { read: true }); | ||
const data = await Deno.readFile(f); | ||
Deno.close(f.rid); | ||
copts.authenticator = credsAuthenticator(data); | ||
} | ||
|
||
if (!argv.bucket) { | ||
console.log("--bucket is required"); | ||
Deno.exit(1); | ||
} | ||
|
||
const nc = await connect(copts); | ||
|
||
const js = nc.jetstream(); | ||
const jsm = await nc.jetstreamManager(); | ||
|
||
// look for the bucket we were given - if not there, we don't attempt | ||
// otherwise we create a store | ||
const lister = jsm.streams.listObjectStores(); | ||
let found = false; | ||
const streamName = `OBJ_${argv.bucket}`; | ||
for await (const oss of lister) { | ||
if (oss.streamInfo.config.name === streamName) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
if (!found) { | ||
console.log(`bucket '${argv.bucket}' was not found`); | ||
Deno.exit(1); | ||
} | ||
|
||
const os = await js.views.os(argv.bucket) as ObjectStoreImpl; | ||
|
||
// get all the metas - the issue is the base64 encoded name in the | ||
// meta doesn't have padding | ||
const osInfo = await os.status({ subjects_filter: "$O.*.M.*" }); | ||
const entries = Object.getOwnPropertyNames( | ||
osInfo.streamInfo.state.subjects || {}, | ||
); | ||
let fixes = 0; | ||
|
||
// check all the entries | ||
for (let i = 0; i < entries.length; i++) { | ||
const chunks = entries[i].split("."); | ||
let key = chunks[3]; | ||
if (key.endsWith("=")) { | ||
// this is already padded | ||
continue; | ||
} | ||
// should be divisible by 4 | ||
let pad = key.length % 4; | ||
if (pad === 0) { | ||
// not needed | ||
continue; | ||
} | ||
// if here we have an entry to fix | ||
fixes++; | ||
// pad so we get to divisible by 4 | ||
pad = 4 - pad; | ||
if (argv.check) { | ||
continue; | ||
} | ||
const padding = pad > 0 ? "=".repeat(pad) : ""; | ||
chunks[3] += padding; | ||
|
||
// this is the correct meta subject | ||
const fixedKey = chunks.join("."); | ||
let m; | ||
try { | ||
// get the entry | ||
m = await jsm.streams.getMessage(os.stream, { | ||
last_by_subj: entries[i], | ||
}); | ||
} catch (err) { | ||
console.error(`[ERR] failed to update ${entries[i]}: ${err.message}`); | ||
continue; | ||
} | ||
// if we have the entry, we can use the same data it has | ||
if (m) { | ||
try { | ||
await js.publish(fixedKey, m.data); | ||
console.log(`[OK] updated ${entries[i]} to ${fixedKey}`); | ||
} catch (err) { | ||
console.error(`[ERR] failed to update ${entries[i]}: ${err.message}`); | ||
continue; | ||
} | ||
|
||
// now we try to delete the old message | ||
// if we fail, the old entry will be there, but at least | ||
// other clients (and updated javascript client) will be | ||
// able to load it | ||
try { | ||
const seq = m.seq; | ||
await jsm.streams.deleteMessage(os.stream, seq); | ||
} catch (err) { | ||
console.error( | ||
`[WARN] failed to delete bad entry ${ | ||
entries[i] | ||
}: ${err.message} - new entry was added`, | ||
); | ||
} | ||
} | ||
} | ||
// print a message of how many entries we updated | ||
const verb = argv.check ? "are" : "were"; | ||
console.log(`${fixes} fixes ${verb} required on bucket ${argv.bucket}`); | ||
|
||
await nc.close(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters