-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to read Java Card details (#3775)
* Remove no longer needed type cast and assertion * Have waitForReadyCardStatus util return ready cardStatus * Export Vx CA cert paths * Add methods to JavaCard class for upcoming read-java-card-details script * Add read-java-card-details script * Add tests to maintain 100% code coverage * Update libs/auth README * Clean up names for clarity * Promisify reader.disconnect and properly await its completion
- Loading branch information
1 parent
d1c7557
commit 6fe593a
Showing
13 changed files
with
237 additions
and
23 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
13 changes: 3 additions & 10 deletions
13
libs/auth/scripts/create_production_machine_cert_signing_request.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env node | ||
|
||
require('esbuild-runner/register'); | ||
require('./read_java_card_details').main(); |
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,85 @@ | ||
import { extractErrorMessage } from '@votingworks/basics'; | ||
|
||
import { | ||
DEV_VX_CERT_AUTHORITY_CERT_PATH, | ||
PROD_VX_CERT_AUTHORITY_CERT_PATH, | ||
} from '../src'; | ||
import { CardDetails } from '../src/card'; | ||
import { JavaCard } from '../src/java_card'; | ||
import { waitForReadyCardStatus } from './utils'; | ||
import { verifyFirstCertWasSignedBySecondCert } from '../src/openssl'; | ||
|
||
const ENVS = ['development', 'production'] as const; | ||
|
||
type Env = typeof ENVS[number]; | ||
|
||
interface ExtendedCardDetails { | ||
cardDetails?: CardDetails; | ||
env: Env; | ||
} | ||
|
||
const VX_CERT_AUTHORITY_CERT_PATHS: Record<Env, string> = { | ||
development: DEV_VX_CERT_AUTHORITY_CERT_PATH, | ||
production: PROD_VX_CERT_AUTHORITY_CERT_PATH, | ||
}; | ||
|
||
async function readJavaCardDetails(): Promise<ExtendedCardDetails | undefined> { | ||
for (const env of ENVS) { | ||
const vxCertAuthorityCertPath = VX_CERT_AUTHORITY_CERT_PATHS[env]; | ||
const card = new JavaCard({ vxCertAuthorityCertPath }); | ||
const { cardDetails } = await waitForReadyCardStatus(card); | ||
if (cardDetails) { | ||
// Card has been run through initial Java Card configuration script and programmed for a user | ||
return { cardDetails, env }; | ||
} | ||
|
||
try { | ||
const cardVxCert = await card.retrieveCertByIdentifier('cardVxCert'); | ||
await verifyFirstCertWasSignedBySecondCert( | ||
cardVxCert, | ||
vxCertAuthorityCertPath | ||
); | ||
// Card has been run through initial Java Card configuration script but not programmed for a | ||
// user | ||
return { env }; | ||
} catch {} /* eslint-disable-line no-empty */ | ||
|
||
// Disconnect the card so that it can be reconnected to, through a new JavaCard instance | ||
await card.disconnect(); | ||
} | ||
|
||
// Card has not been run through initial Java Card configuration script | ||
return undefined; | ||
} | ||
|
||
function formatCardDetails(extendedCardDetails?: ExtendedCardDetails): string { | ||
const { cardDetails, env } = extendedCardDetails ?? {}; | ||
const { jurisdiction, role } = cardDetails?.user ?? {}; | ||
const electionHash = | ||
cardDetails?.user.role !== 'system_administrator' | ||
? cardDetails?.user.electionHash | ||
: undefined; | ||
return ` | ||
Env: ${env ?? '-'} | ||
Jurisdiction: ${jurisdiction ?? '-'} | ||
User role: ${role ?? '-'} | ||
Election hash: ${electionHash ?? '-'} | ||
`; | ||
} | ||
|
||
/** | ||
* A script for reading Java Card details, namely environment, jurisdiction, user role, and | ||
* election hash | ||
*/ | ||
export async function main(): Promise<void> { | ||
let formattedCardDetails: string; | ||
try { | ||
const cardDetails = await readJavaCardDetails(); | ||
formattedCardDetails = formatCardDetails(cardDetails); | ||
} catch (error) { | ||
console.error(`❌ ${extractErrorMessage(error)}`); | ||
process.exit(1); | ||
} | ||
console.log(formattedCardDetails); | ||
process.exit(0); | ||
} |
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
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
Oops, something went wrong.