-
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
6 changed files
with
194 additions
and
27 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,149 @@ | ||
/* | ||
* 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, | ||
JSONCodec, | ||
} 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 entries in an object store that were written", | ||
); | ||
console.log( | ||
"with base64 encoding without padding. Please backup your object stores", | ||
); | ||
console.log("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(); | ||
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; | ||
|
||
// `$${osPrefix}${os.name}.M.>` | ||
const osInfo = await os.status({ subjects_filter: "$O.*.M.*" }); | ||
const entries = Object.getOwnPropertyNames( | ||
osInfo.streamInfo.state.subjects || {}, | ||
); | ||
let fixes = 0; | ||
|
||
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; | ||
} | ||
const pad = key.length % 4; | ||
if (pad === 0) { | ||
continue; | ||
} | ||
// this entry is incorrect fix it | ||
fixes++; | ||
if (argv.check) { | ||
continue; | ||
} | ||
const padding = pad > 0 ? "=".repeat(pad) : ""; | ||
chunks[3] += padding; | ||
const fixedKey = chunks.join("."); | ||
|
||
let m; | ||
try { | ||
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 (m) { | ||
try { | ||
await js.publish(fixedKey, m.data); | ||
} catch (err) { | ||
console.error(`[ERR] failed to update ${entries[i]}: ${err.message}`); | ||
continue; | ||
} | ||
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`, | ||
); | ||
} | ||
} | ||
} | ||
|
||
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
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