Skip to content

Commit

Permalink
Read authentication type for connection profile
Browse files Browse the repository at this point in the history
Change-type: patch
Signed-off-by: Ken Bannister <kb2ma@runbox.com>
  • Loading branch information
kb2ma committed Jul 19, 2023
1 parent 181afa9 commit 7b480c7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
35 changes: 31 additions & 4 deletions lib/migrator/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,35 +200,62 @@ export const getTargetBlockDevice = async (mountLabel: string = 'C') => {
})
}

/**
* Identifies the type of WiFi authentication for a connection profile.
* NONE means no authentication.
*/
export const enum WifiAuthType { NONE, WPA2_PSK, WPA3_SAE }

/** Configuration values for a single WiFi network profile. */
export interface ConnectionProfile {
name: string;
wifiSsid: string;
wifiAuthType: WifiAuthType;
wifiKey: string;
}

/** Generates the contents for a balenaOS network configuration file. */
const generateWifiConfig = (profile: ConnectionProfile): string => {
return `[connection]
const connectionSection = `[connection]
id=${profile.name}
type=wifi
`
const wifiSection = `
[wifi]
mode=infrastructure
ssid=${profile.wifiSsid}
`
let keyMgmt
switch (profile.wifiAuthType) {
case WifiAuthType.NONE:
keyMgmt = 'none'
break
case WifiAuthType.WPA2_PSK:
keyMgmt = 'wpa-psk'
break
case WifiAuthType.WPA3_SAE:
keyMgmt = 'sae'
throw Error("WifiAuthType WPA3_SAE not supported")
default:
throw Error(`WifiAuthType ${profile.wifiAuthType} not defined`)
}
const wifiSecuritySection = `
[wifi-security]
auth-alg=open
key-mgmt=wpa-psk
key-mgmt=${keyMgmt}
psk=${profile.wifiKey}
`

const ipSection = `
[ipv4]
method=auto
[ipv6]
addr-gen-mode=stable-privacy
method=auto
`

return connectionSection.concat(wifiSection, wifiSecuritySection, ipSection)
}

/**
Expand Down
7 changes: 7 additions & 0 deletions lib/migrator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
findNewPartitions,
findFilesystemLabel,
ConnectionProfile,
WifiAuthType,
writeNetworkConfig
} from './helpers';
import { copyBootloaderFromImage } from './copy-bootloader';
Expand All @@ -22,6 +23,9 @@ const execAsync = promisify(childExec);
import { constants as osConstants } from 'os';
import { existsSync } from 'fs'

// Callers need to provide profiles via options
export { ConnectionProfile, WifiAuthType }

/** Determine if running as administrator. */
async function isElevated(): Promise<boolean> {
// `fltmc` is available on WinPE, XP, Vista, 7, 8, and 10
Expand Down Expand Up @@ -282,6 +286,9 @@ export const migrate = async (
await writeNetworkConfig(pathname, profile)
console.log(`Wrote network configuration for ${profile.name}`)
}
if (!options.connectionProfiles.length) {
console.log("No network configuration provided")
}
} finally {
await diskpart.clearDriveLetter(bootPartition.volumeId, driveLetter)
}
Expand Down

0 comments on commit 7b480c7

Please sign in to comment.