Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show CA "Needs attention" #201

Merged
merged 3 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ class MainActivity: FlutterActivity() {
return result.error("unhandled_error", err.message, null)
}

if (!validateOrDeleteSite(siteDir)) {
val validationResult = validateOrDeleteSite(siteDir);
if (validationResult is ValidationResult.FailureReason) {
return result.error("failure", validationResult.value, null)
} else if (validationResult is ValidationResult.BooleanResult && !validationResult.value) {
return result.error("failure", "Enrollment failed due to invalid config", null)
}

Expand Down Expand Up @@ -225,7 +228,10 @@ class MainActivity: FlutterActivity() {
return result.error("failure", err.toString(), null)
}

if (!validateOrDeleteSite(siteDir)) {
val validationResult = validateOrDeleteSite(siteDir);
if (validationResult is ValidationResult.FailureReason) {
return result.error("failure", validationResult.value, null)
} else if (validationResult is ValidationResult.BooleanResult && !validationResult.value) {
return result.error("failure", "Site config was incomplete, please review and try again", null)
}

Expand All @@ -234,19 +240,28 @@ class MainActivity: FlutterActivity() {
result.success(null)
}

private fun validateOrDeleteSite(siteDir: File): Boolean {
sealed class ValidationResult {
data class FailureReason(val value: String) : ValidationResult()
data class BooleanResult(val value: Boolean) : ValidationResult()
}

private fun validateOrDeleteSite(siteDir: File): ValidationResult {
try {
// Try to render a full site, if this fails the config was bad somehow
Site(context, siteDir)
val site = Site(context, siteDir);
if (site.errors.isNotEmpty()) {
// Return the first error, so the user can at least know one thing to solve
return ValidationResult.FailureReason(site.errors[0])
}
} catch(err: java.io.FileNotFoundException) {
Log.e(TAG, "Site not found at $siteDir")
return false
return ValidationResult.BooleanResult(false)
} catch(err: Exception) {
Log.e(TAG, "Deleting site at $siteDir due to error: $err")
siteDir.deleteRecursively()
return false
return ValidationResult.BooleanResult(false)
}
return true
return ValidationResult.BooleanResult(true)
}

private fun startSite(call: MethodCall, result: MethodChannel.Result) {
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/siteConfig/SiteConfigScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class _SiteConfigScreenState extends State<SiteConfigScreen> {
final certError = site.certInfo == null || site.certInfo!.validity == null || !site.certInfo!.validity!.valid;
var caError = false;
if (!site.managed) {
var caError = site.ca.length == 0;
caError = site.ca.length == 0;
if (!caError) {
site.ca.forEach((ca) {
if (ca.validity == null || !ca.validity!.valid) {
Expand Down
Loading