|
1 | 1 | import React from "react"; |
2 | 2 |
|
3 | 3 | import * as protos from "src/js/protos"; |
| 4 | +import _ from "lodash"; |
| 5 | +import Long from "long"; |
| 6 | +import moment from "moment"; |
4 | 7 | import { EncryptionStatusProps } from "oss/src/views/reports/containers/stores/encryption"; |
| 8 | +import { Bytes } from "src/util/format"; |
| 9 | +import { FixLong } from "src/util/fixLong"; |
| 10 | + |
| 11 | +const dateFormat = "Y-MM-DD HH:mm:ss"; |
5 | 12 |
|
6 | 13 | export default class EncryptionStatus extends React.Component<EncryptionStatusProps, {}> { |
7 | 14 |
|
| 15 | + renderHeaderRow(header: string) { |
| 16 | + return ( |
| 17 | + <tr className="stores-table__row"> |
| 18 | + <td colSpan={2} className="stores-table__cell stores-table__cell--header--row">{header}</td> |
| 19 | + </tr> |
| 20 | + ); |
| 21 | + } |
| 22 | + |
8 | 23 | renderSimpleRow(header: string, value: string) { |
9 | 24 | return ( |
10 | 25 | <tr className="stores-table__row"> |
11 | 26 | <th className="stores-table__cell stores-table__cell--header">{header}</th> |
12 | | - <td className="stores-table__cell" title={value}><pre>{value}</pre></td> |
| 27 | + <td className="stores-table__cell" title={value}>{value}</td> |
13 | 28 | </tr> |
14 | 29 | ); |
15 | 30 | } |
16 | 31 |
|
17 | | - render(): React.ReactElement<any> { |
18 | | - const { store } = this.props; |
19 | | - const rawStatus = store.encryption_status; |
| 32 | + renderStoreKey(key: protos.cockroach.ccl.storageccl.engineccl.enginepbccl.IKeyInfo) { |
| 33 | + // Get the enum name from its value (eg: "AES128_CTR" for 1). |
| 34 | + const encryptionType = protos.cockroach.ccl.storageccl.engineccl.enginepbccl.EncryptionType[key.encryption_type]; |
| 35 | + const createdAt = moment.unix(FixLong(key.creation_time).toNumber()).utc().format(dateFormat); |
| 36 | + |
| 37 | + return [ |
| 38 | + this.renderHeaderRow("Active Store Key: user specified"), |
| 39 | + this.renderSimpleRow("Algorithm", encryptionType), |
| 40 | + this.renderSimpleRow("Key ID", key.key_id), |
| 41 | + this.renderSimpleRow("Created", createdAt), |
| 42 | + this.renderSimpleRow("Source", key.source), |
| 43 | + ]; |
| 44 | + } |
| 45 | + |
| 46 | + renderDataKey(key: protos.cockroach.ccl.storageccl.engineccl.enginepbccl.IKeyInfo) { |
| 47 | + // Get the enum name from its value (eg: "AES128_CTR" for 1). |
| 48 | + const encryptionType = protos.cockroach.ccl.storageccl.engineccl.enginepbccl.EncryptionType[key.encryption_type]; |
| 49 | + const createdAt = moment.unix(key.creation_time.toNumber()).utc().format(dateFormat); |
| 50 | + |
| 51 | + return [ |
| 52 | + this.renderHeaderRow("Active Data Key: automatically generated"), |
| 53 | + this.renderSimpleRow("Algorithm", encryptionType), |
| 54 | + this.renderSimpleRow("Key ID", key.key_id), |
| 55 | + this.renderSimpleRow("Created", createdAt), |
| 56 | + this.renderSimpleRow("Parent Key ID", key.parent_key_id), |
| 57 | + ]; |
| 58 | + } |
| 59 | + |
| 60 | + calculatePercentage(active: Long, total: Long): number { |
| 61 | + if (active === total) { |
| 62 | + return 100; |
| 63 | + } |
| 64 | + return Long.fromInt(100).mul(active).toNumber() / total.toNumber(); |
| 65 | + } |
| 66 | + |
| 67 | + renderFileStats(stats: protos.cockroach.server.serverpb.IStoreDetails) { |
| 68 | + const totalFiles = FixLong(stats.total_files); |
| 69 | + const totalBytes = FixLong(stats.total_bytes); |
| 70 | + if (totalFiles.eq(0) && totalBytes.eq(0)) { |
| 71 | + return null; |
| 72 | + } |
| 73 | + |
| 74 | + const activeFiles = FixLong(stats.active_key_files); |
| 75 | + const activeBytes = FixLong(stats.active_key_bytes); |
| 76 | + |
| 77 | + let fileDetails = this.calculatePercentage(activeFiles, totalFiles).toFixed(2) + "%"; |
| 78 | + fileDetails += " (" + activeFiles + "/" + totalFiles + ")"; |
| 79 | + |
| 80 | + let byteDetails = this.calculatePercentage(activeBytes, totalBytes).toFixed(2) + "%"; |
| 81 | + byteDetails += " (" + Bytes(activeBytes.toNumber()) + "/" + Bytes(totalBytes.toNumber()) + ")"; |
20 | 82 |
|
| 83 | + return [ |
| 84 | + this.renderHeaderRow("Encryption Progress: fraction encrypted using the active data key"), |
| 85 | + this.renderSimpleRow("Files", fileDetails), |
| 86 | + this.renderSimpleRow("Bytes", byteDetails), |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + decodeEncryptionStatus(data: Uint8Array): protos.cockroach.ccl.storageccl.engineccl.enginepbccl.EncryptionStatus { |
| 91 | + let decodedStatus; |
| 92 | + |
| 93 | + // Attempt to decode protobuf. |
21 | 94 | try { |
22 | | - const decodedStatus = protos.cockroach.ccl.storageccl.engineccl.enginepbccl.EncryptionStatus.decode(rawStatus); |
23 | | - return this.renderSimpleRow("Encryption Status", JSON.stringify(decodedStatus.toJSON(), null, 2)); |
| 95 | + decodedStatus = protos.cockroach.ccl.storageccl.engineccl.enginepbccl.EncryptionStatus.decode(data); |
24 | 96 | } catch (e) { |
25 | 97 | console.log("Error decoding protobuf: ", e); |
26 | 98 | return null; |
27 | 99 | } |
| 100 | + return decodedStatus; |
| 101 | + } |
| 102 | + |
| 103 | + getEncryptionRows() { |
| 104 | + const { store } = this.props; |
| 105 | + const rawStatus = store.encryption_status; |
| 106 | + if (_.isEmpty(rawStatus)) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + const decodedStatus = this.decodeEncryptionStatus(rawStatus); |
| 111 | + if (decodedStatus == null) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + return [ |
| 116 | + this.renderStoreKey(decodedStatus.active_store_key), |
| 117 | + this.renderDataKey(decodedStatus.active_data_key), |
| 118 | + this.renderFileStats(store), |
| 119 | + ]; |
28 | 120 | } |
29 | 121 | } |
0 commit comments