|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Red Hat, Inc. |
| 3 | + * This program and the accompanying materials are made |
| 4 | + * available under the terms of the Eclipse Public License 2.0 |
| 5 | + * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * Red Hat, Inc. - initial API and implementation |
| 11 | + */ |
| 12 | +package com.redhat.devtools.gateway.kubeconfig |
| 13 | + |
| 14 | +import io.kubernetes.client.util.KubeConfig |
| 15 | +import kotlin.collections.get |
| 16 | + |
| 17 | +/** |
| 18 | + * Domain classes representing the structure of a kubeconfig file. |
| 19 | + */ |
| 20 | +data class KubeConfigNamedCluster( |
| 21 | + val name: String, |
| 22 | + val cluster: KubeConfigCluster |
| 23 | +) { |
| 24 | + companion object { |
| 25 | + fun fromMap(name: String, clusterObject: Any?): KubeConfigNamedCluster? { |
| 26 | + val clusterMap = clusterObject as? Map<*, *> ?: return null |
| 27 | + val clusterDetails = clusterMap["cluster"] as? Map<*, *> ?: return null |
| 28 | + |
| 29 | + return KubeConfigNamedCluster( |
| 30 | + name = name, |
| 31 | + cluster = KubeConfigCluster.fromMap(clusterDetails) ?: return null |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + fun fromKubeConfig(kubeConfig: KubeConfig): List<KubeConfigNamedCluster> { |
| 36 | + return (kubeConfig.clusters as? List<*>) |
| 37 | + ?.mapNotNull { clusterObject -> |
| 38 | + val clusterMap = clusterObject as? Map<*, *> ?: return@mapNotNull null |
| 39 | + val name = clusterMap["name"] as? String ?: return@mapNotNull null |
| 40 | + fromMap(name, clusterObject) |
| 41 | + } ?: emptyList() |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +data class KubeConfigCluster( |
| 47 | + val server: String, |
| 48 | + val certificateAuthorityData: String? = null, |
| 49 | + val insecureSkipTlsVerify: Boolean? = null |
| 50 | +) { |
| 51 | + companion object { |
| 52 | + fun fromMap(map: Map<*, *>): KubeConfigCluster? { |
| 53 | + val server = map["server"] as? String ?: return null |
| 54 | + return KubeConfigCluster( |
| 55 | + server = server, |
| 56 | + certificateAuthorityData = map["certificate-authority-data"] as? String, |
| 57 | + insecureSkipTlsVerify = map["insecure-skip-tls-verify"] as? Boolean |
| 58 | + ) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +data class KubeConfigNamedContext( |
| 64 | + val name: String, |
| 65 | + val context: KubeConfigContext |
| 66 | +) { |
| 67 | + companion object { |
| 68 | + fun getByClusterName(clusterName: String, kubeConfig: KubeConfig): KubeConfigNamedContext? { |
| 69 | + return (kubeConfig.contexts as? List<*>)?.firstNotNullOfOrNull { contextObject -> |
| 70 | + val contextMap = contextObject as? Map<*, *> ?: return@firstNotNullOfOrNull null |
| 71 | + val contextName = contextMap["name"] as? String ?: return@firstNotNullOfOrNull null |
| 72 | + val contextEntry = getByName(contextName, contextObject) |
| 73 | + if (contextEntry?.context?.cluster == clusterName) { |
| 74 | + contextEntry |
| 75 | + } else { |
| 76 | + null |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private fun getByName(name: String, contextObject: Any?): KubeConfigNamedContext? { |
| 82 | + val contextMap = contextObject as? Map<*, *> ?: return null |
| 83 | + val contextDetails = contextMap["context"] as? Map<*, *> ?: return null |
| 84 | + |
| 85 | + return KubeConfigNamedContext( |
| 86 | + name = name, |
| 87 | + context = KubeConfigContext.fromMap(contextDetails) ?: return null |
| 88 | + ) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +data class KubeConfigContext( |
| 94 | + val cluster: String, |
| 95 | + val user: String, |
| 96 | + val namespace: String? = null |
| 97 | +) { |
| 98 | + companion object { |
| 99 | + fun fromMap(map: Map<*, *>): KubeConfigContext? { |
| 100 | + val cluster = map["cluster"] as? String ?: return null |
| 101 | + val user = map["user"] as? String ?: return null |
| 102 | + |
| 103 | + return KubeConfigContext( |
| 104 | + cluster = cluster, |
| 105 | + user = user, |
| 106 | + namespace = map["namespace"] as? String |
| 107 | + ) |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +data class KubeConfigNamedUser( |
| 113 | + val name: String, |
| 114 | + val user: KubeConfigUser |
| 115 | +) { |
| 116 | + companion object { |
| 117 | + fun fromMap(name: String, userObject: Any?): KubeConfigNamedUser? { |
| 118 | + val userMap = userObject as? Map<*, *> ?: return null |
| 119 | + val userDetails = userMap["user"] as? Map<*, *> ?: return null |
| 120 | + |
| 121 | + return KubeConfigNamedUser( |
| 122 | + name = name, |
| 123 | + user = KubeConfigUser.fromMap(userDetails) |
| 124 | + ) |
| 125 | + } |
| 126 | + |
| 127 | + fun getUserTokenForCluster(clusterName: String, kubeConfig: KubeConfig): String? { |
| 128 | + val contextEntry = KubeConfigNamedContext.getByClusterName(clusterName, kubeConfig) ?: return null |
| 129 | + val userObject = (kubeConfig.users as? List<*>)?.firstOrNull { userObject -> |
| 130 | + val userMap = userObject as? Map<*, *> ?: return@firstOrNull false |
| 131 | + val userName = userMap["name"] as? String ?: return@firstOrNull false |
| 132 | + userName == contextEntry.context.user |
| 133 | + } ?: return null |
| 134 | + return fromMap(contextEntry.context.user, userObject)?.user?.token |
| 135 | + } |
| 136 | + |
| 137 | + fun isTokenAuth(kubeConfig: KubeConfig): Boolean { |
| 138 | + return kubeConfig.credentials?.containsKey(KubeConfig.CRED_TOKEN_KEY) == true |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +data class KubeConfigUser( |
| 144 | + val token: String? = null, |
| 145 | + val clientCertificateData: String? = null, |
| 146 | + val clientKeyData: String? = null, |
| 147 | + val username: String? = null, |
| 148 | + val password: String? = null |
| 149 | +) { |
| 150 | + companion object { |
| 151 | + fun fromMap(map: Map<*, *>): KubeConfigUser { |
| 152 | + return KubeConfigUser( |
| 153 | + token = map["token"] as? String, |
| 154 | + clientCertificateData = map["client-certificate-data"] as? String, |
| 155 | + clientKeyData = map["client-key-data"] as? String, |
| 156 | + username = map["username"] as? String, |
| 157 | + password = map["password"] as? String |
| 158 | + ) |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
0 commit comments