11import React from "react" ;
22
33import * as protos from "src/js/protos" ;
4+ import _ from "lodash" ;
5+ import Long from "long" ;
6+ import moment from "moment" ;
47import { EncryptionStatusProps } from "oss/src/views/reports/containers/stores/encryption" ;
8+ import { Bytes } from "src/util/format" ;
9+
10+ const dateFormat = "Y-MM-DD HH:mm:ss Z" ;
511
612export default class EncryptionStatus extends React . Component < EncryptionStatusProps , { } > {
713
14+ renderHeaderRow ( header : string ) {
15+ return (
16+ < tr className = "stores-table__row" >
17+ < td colspan = 2 className = "stores-table__cell stores-table__cell--header--row" > { header } </ td >
18+ </ tr >
19+ ) ;
20+ }
21+
822 renderSimpleRow ( header : string , value : string ) {
923 return (
1024 < tr className = "stores-table__row" >
@@ -14,16 +28,77 @@ export default class EncryptionStatus extends React.Component<EncryptionStatusPr
1428 ) ;
1529 }
1630
17- render ( ) : React . ReactElement < any > {
31+ renderKey ( isStoreKey : boolean , key : protos . cockroach . ccl . storageccl . engineccl . enginepbccl . KeyInfo$Properties ) {
32+ // Get the enum name from its value (eg: "AES128_CTR" for 1).
33+ const encryptionType = protos . cockroach . ccl . storageccl . engineccl . enginepbccl . EncryptionType [ key . encryption_type ] ;
34+ const createdAt = moment . unix ( key . creation_time . toNumber ( ) ) . utc ( ) . format ( dateFormat ) ;
35+
36+ if ( isStoreKey ) {
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+ } else {
45+ return [
46+ this . renderHeaderRow ( "Active Data Key: automatically generated" ) ,
47+ this . renderSimpleRow ( "Algorithm" , encryptionType ) ,
48+ this . renderSimpleRow ( "Key ID" , key . key_id ) ,
49+ this . renderSimpleRow ( "Created" , createdAt ) ,
50+ this . renderSimpleRow ( "Parent Key ID" , key . parent_key_id ) ,
51+ ] ;
52+ }
53+ }
54+
55+ renderFileStats ( stats : protos . cockroach . server . serverpb . StoreDetails$Properties ) {
56+ if ( stats . total_files . eq ( 0 ) && stats . total_bytes . eq ( 0 ) ) {
57+ return null ;
58+ }
59+
60+ let percentFiles = 100 ;
61+ if ( stats . active_key_files !== stats . total_files ) {
62+ percentFiles = Long . fromInt ( 100 ) . mul ( stats . active_key_files ) . toNumber ( ) / stats . total_files . toNumber ( ) ;
63+ }
64+ let fileDetails = percentFiles . toFixed ( 2 ) + "%" ;
65+ fileDetails += " (" + stats . active_key_files + "/" + stats . total_files + ")" ;
66+
67+ let percentBytes = 100 ;
68+ if ( stats . active_key_bytes !== stats . total_bytes ) {
69+ percentBytes = Long . fromInt ( 100 ) . mul ( stats . active_key_bytes ) . toNumber ( ) / stats . total_bytes . toNumber ( ) ;
70+ }
71+ let byteDetails = percentBytes . toFixed ( 2 ) + "%" ;
72+ byteDetails += " (" + Bytes ( stats . active_key_bytes . toNumber ( ) ) + "/" + Bytes ( stats . total_bytes . toNumber ( ) ) + ")" ;
73+
74+ return [
75+ this . renderHeaderRow ( "Encryption Progress: fraction encrypted using the active data key" ) ,
76+ this . renderSimpleRow ( "Files" , fileDetails ) ,
77+ this . renderSimpleRow ( "Bytes" , byteDetails ) ,
78+ ] ;
79+ }
80+
81+ render ( ) {
1882 const { store } = this . props ;
1983 const rawStatus = store . encryption_status ;
84+ if ( _ . isEmpty ( rawStatus ) ) {
85+ return null ;
86+ }
87+
88+ let decodedStatus ;
2089
90+ // Attempt to decode protobuf.
2191 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 ) ) ;
92+ decodedStatus = protos . cockroach . ccl . storageccl . engineccl . enginepbccl . EncryptionStatus . decode ( rawStatus ) ;
2493 } catch ( e ) {
2594 console . log ( "Error decoding protobuf: " , e ) ;
2695 return null ;
2796 }
97+
98+ return [
99+ this . renderKey ( true , decodedStatus . active_store_key ) ,
100+ this . renderKey ( false , decodedStatus . active_data_key ) ,
101+ this . renderFileStats ( store ) ,
102+ ] ;
28103 }
29104}
0 commit comments