Skip to content

Commit

Permalink
feat: update setGiteaOIDCConfig flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ferruhcihan committed Aug 16, 2024
1 parent 6c2216d commit 9695254
Showing 1 changed file with 54 additions and 35 deletions.
89 changes: 54 additions & 35 deletions src/operator/gitea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,23 @@ if (typeof require !== 'undefined' && require.main === module) {
}

// Runners
const createSetGiteaOIDCConfigRunner = (() => {
let intervalId: any = null
return function runSetGiteaOIDCConfig() {
if (intervalId === null) {
intervalId = setInterval(() => {
setGiteaOIDCConfig()
.catch((error) => {
console.error('Error occurred during setGiteaOIDCConfig execution:', error)
})
.finally(() => {
intervalId = null
})
}, 30 * 1000)
}
}
})()

async function checkAndExecute() {
const currentState: DependencyState = {
giteaPassword: env.giteaPassword,
Expand All @@ -186,17 +203,8 @@ async function checkAndExecute() {
await setupGitea()
}

// Check and execute setGiteaOIDCConfig if dependencies changed
if (
!currentState.oidcClientId ||
!currentState.oidcClientSecret ||
!currentState.oidcEndpoint ||
currentState.oidcClientId !== lastState.oidcClientId ||
currentState.oidcClientSecret !== lastState.oidcClientSecret ||
currentState.oidcEndpoint !== lastState.oidcEndpoint
) {
await setGiteaOIDCConfig()
}
// Interval to check and update the Gitea OIDC config
createSetGiteaOIDCConfigRunner()

// Check and execute setGiteaGroupMapping if dependencies changed
if (!currentState.teamNames || currentState.teamNames !== lastState.teamNames) {
Expand Down Expand Up @@ -476,43 +484,54 @@ async function setGiteaOIDCConfig() {
const clientSecret = env.oidcClientSecret
const discoveryURL = `${env.oidcEndpoint}/.well-known/openid-configuration`

const isConfigDifferent =
lastState.oidcClientId !== env.oidcClientId || lastState.oidcClientSecret !== env.oidcClientSecret

try {
const execCommand = [
'sh',
'-c',
`
AUTH_ID=$(gitea admin auth list --vertical-bars | grep -E "\\|otomi-idp\\s+\\|" | grep -iE "\\|OAuth2\\s+\\|" | awk -F " " '{print $1}' | tr -d '\\n')
if [ -z "$AUTH_ID" ]; then
echo "Gitea OIDC config not found. Adding OIDC config for otomi-idp."
gitea admin auth add-oauth --name "otomi-idp" --key "${clientID}" --secret "${clientSecret}" --auto-discover-url "${discoveryURL}" --provider "openidConnect" --admin-group "team-admin" --group-claim-name "groups"
else
elif [ "${isConfigDifferent}" = "true" ]; then
echo "Gitea OIDC config is different. Updating OIDC config for otomi-idp."
gitea admin auth update-oauth --id "$AUTH_ID" --key "${clientID}" --secret "${clientSecret}" --auto-discover-url "${discoveryURL}"
else
echo "Gitea OIDC config is up to date."
fi
`,
]
if (podNamespace && podName) {
const exec = new k8s.Exec(kc)
// Run gitea CLI command to create/update the gitea oauth configuration
await exec
.exec(
podNamespace,
podName,
'gitea',
execCommand,
null,
process.stderr as stream.Writable,
process.stdin as stream.Readable,
false,
(status: k8s.V1Status) => {
console.info('Gitea OIDC configuration status:', status.status)
},
)
.catch((error) => {
console.debug('Error occurred during exec:', error)
throw error
})
}
const exec = new k8s.Exec(kc)
const outputStream = new stream.PassThrough()
let output = ''
outputStream.on('data', (chunk) => {
output += chunk.toString()
})
// Run gitea CLI command to create/update the gitea oauth configuration
await exec
.exec(
podNamespace,
podName,
'gitea',
execCommand,
outputStream,
process.stderr as stream.Writable,
process.stdin as stream.Readable,
false,
(status: k8s.V1Status) => {
console.info(output.trim())
console.info('Gitea OIDC config status:', status.status)
},
)
.catch((error) => {
console.debug('Error occurred during exec:', error)
throw error
})
} catch (error) {
console.debug(`Error Gitea OIDC configuration: ${error.message}`)
console.debug(`Error Gitea OIDC config: ${error.message}`)
throw error
}
}

0 comments on commit 9695254

Please sign in to comment.