Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-74313

[conjur-credentials] Extract inline script block in org/conjur/jenkins/configuration/GlobalConjurConfiguration/config.jelly

XMLWordPrintable

      Problem

      == Inline Script Block
      Line: 85
      ----
      <script type="text/javascript">
            // Function to set value based on checkbox state
            document.getElementById('enableIdentityFormatFieldsFromToken').addEventListener('change', getIdentityFormatToken);
            document.getElementById('listIdentityFieldsSeparator').addEventListener('change', selectedFieldSeparator);
      
      
      
            function getIdentityFormatToken() {
                var selectJwtAudienceValue ='${instance.jwtAudience}';
                var identityFieldNameDefault ='${instance.identityFieldName}';
                var selectIdentityFormatTokenValue ='${instance.selectIdentityFormatToken}';
                identityFieldNameDefault =identityFieldNameDefault ? identityFieldNameDefault : 'sub';
                selectIdentityFormatTokenValue =selectIdentityFormatTokenValue ? selectIdentityFormatTokenValue : 'jenkins_full_name';
                var selectIdentityFieldsSeparatorValue = '${instance.selectIdentityFieldsSeparator}';
                  // Set default value if selectIdentityFieldsSeparator is empty
                selectIdentityFieldsSeparatorValue = selectIdentityFieldsSeparatorValue ? selectIdentityFieldsSeparatorValue : '-';
      
                var jwtAudienceDefault = document.getElementsByName('jwtAudience')[0];
      
                var inputEnableIdentityCheckedToken = document.getElementById('enableIdentityFormatFieldsFromToken');
                var divTxtIdentityFieldName =document.getElementById('divIdentityFieldName');
                var txtIdentityFieldName = document.getElementById('identityFieldName');
                var divSelectedIdentityFormatFiledToken = document.getElementById("divListIdentityFormatFieldsFromToken");
                var selectedIdentityFormatFiledToken = document.getElementById("listIdentityFormatFieldsFromToken");
                var inputIdentityToken = document.getElementById("identityFormatFieldsFromToken");
                var divSelectedIdentityFieldsSeparator = document.getElementById("divIdentityFieldsSeparator");
                var selectedIdentityFieldsSeparator = document.getElementById("listIdentityFieldsSeparator");
                var inputIdentityFieldsSeparator = document.getElementById("identityFieldsSeparator");
                var divWarningMsg = document.getElementById("dviWarning");
                var divWarningTxtIdentityTokenMsg = document.getElementById("divTxtWarningIdentityFieldToken");
                var divWarningSeparatorMsg = document.getElementById("divWarningSeparator");
                var divJwtAudWarningMsg =document.getElementById("divJwtAudWarning");
      
      
                if(selectedIdentityFieldsSeparator.value =='-'){
                     divWarningSeparatorMsg.style.display = "none";
                }else{
                     divWarningSeparatorMsg.style.display = "block";
                  }
                // If checkbox is checked, set default textbox value, otherwise clear its value
                if (inputEnableIdentityCheckedToken.checked) {
                    divTxtIdentityFieldName.style.display = "none";
                    txtIdentityFieldName.style.display = "none";
                    divSelectedIdentityFormatFiledToken.style.display = "block";
                    inputIdentityToken.style.display = "none";
                    divSelectedIdentityFieldsSeparator.style.display = "none";
                    inputIdentityFieldsSeparator.style.display = "none";
                    selectedIdentityFormatFiledToken.value=selectIdentityFormatTokenValue;
                    divWarningMsg.style.display = "none";
                    divWarningSeparatorMsg.style.display = "none";
                    jwtAudienceDefault.value = "cyberark-conjur";
                    jwtAudienceDefault.style.color = "#666666"; // Set color to gray
                    jwtAudienceDefault.readOnly = true;
                    divJwtAudWarningMsg.style.display = "none";
                    divWarningTxtIdentityTokenMsg.style.display ="none";
                } else {
                    divTxtIdentityFieldName.style.display = "block";
                    txtIdentityFieldName.style.display = "block";
                    divSelectedIdentityFormatFiledToken.style.display = "none";
                    inputIdentityToken.style.display = "block";
                    divSelectedIdentityFieldsSeparator.style.display = "block";
                    inputIdentityFieldsSeparator.style.display = "none";
                    selectedIdentityFieldsSeparator.value=selectIdentityFieldsSeparatorValue;
                    divWarningMsg.style.display = "block";
                    jwtAudienceDefault.readOnly = false;
                    jwtAudienceDefault.style.color = ""; // Set color to gray
                    divJwtAudWarningMsg.style.display = "block";
                    jwtAudienceDefault.value =selectJwtAudienceValue;
                    txtIdentityFieldName.value =identityFieldNameDefault;
                    divWarningTxtIdentityTokenMsg.style.display ="block";
                }
            }
            // Call the function initially to set the initial state based on checkbox
            getIdentityFormatToken();
              document.getElementById('jwtAudience').addEventListener('change', disableJwtAudienceInput);
              document.addEventListener("input", disableSubmitButton);
      
              function disableJwtAudienceInput(){
                  var inputEnableIdentityCheckedToken = document.getElementById('enableIdentityFormatFieldsFromToken');
                  var jwtAudienceDefault = document.getElementsByName('jwtAudience')[0];
                  var divJwtAudWarningMsg =document.getElementById("divJwtAudWarning");
                      if(inputEnableIdentityCheckedToken.checked){
                      jwtAudienceDefault.value = "cyberark-conjur";
                      jwtAudienceDefault.style.color = "#666666"; // Set color to gray
                      jwtAudienceDefault.readOnly = true;
                      divJwtAudWarningMsg.style.display = "none";
      
                  }else{
                      jwtAudienceDefault.readOnly = false;
                      jwtAudienceDefault.style.color = ""; // reset
                      divJwtAudWarningMsg.style.display = "block";
                  }
              }
      
             function disableSubmitButton(){
                  var inputAuthWebServiceId = document.getElementsByName('authWebServiceId')[0];
                  var inputJwtAudience = document.getElementsByName('jwtAudience')[0];
      
                  var inputKeyLifetimeInMinutes = document.getElementsByName('keyLifetimeInMinutes')[0];
                  var inputTokenDurarionInSeconds = document.getElementsByName('tokenDurarionInSeconds')[0];
      
                  var inputIdentityFieldName= document.getElementsByName('identityFieldName')[0];
                  var inputIdentityFormatFieldsFromToken= document.getElementsByName('identityFormatFieldsFromToken')[0];
      
                  var applyBtn = document.querySelector('button[name="Apply"]');
                  var submitBtn = document.querySelector('button[name="Submit"]');
                 if (inputAuthWebServiceId.value.trim() === '' || inputJwtAudience.value.trim() === '' || inputIdentityFieldName.value.trim() === '' || inputIdentityFormatFieldsFromToken.value.trim() === '' || inputKeyLifetimeInMinutes.value.trim() === '' || inputTokenDurarionInSeconds.value.trim() === '') {
                      applyBtn.disabled = true;
                      submitBtn.disabled = true;
                   }else{
                       applyBtn.disabled = false;
                       submitBtn.disabled = false;
                   }
               }
               disableSubmitButton();
               disableJwtAudienceInput();
               function selectedFieldSeparator(SelectedSeparatorValue){
                   var onChangeWarningSeparatorMsg = document.getElementById("dviWarningSeperator");
                     if(SelectedSeparatorValue.target.value=='-'){
                     onChangeWarningSeparatorMsg.style.display = "none";
                    }else{
                     onChangeWarningSeparatorMsg.style.display = "block";
                  }
               }
               //New setUp Integration for JWT Auth Config
             function enableIdentityFromToken(){
                 var inputEnableJWKS = '${instance.enableJWKS}';
                 var inputAuthWebServiceId = '${instance.authWebServiceId}';
                 var inputJwtAudience = '${instance.jwtAudience}';
                 var inputKeyLifetimeInMinutes = '${instance.keyLifetimeInMinutes}';
                 var inputTokenDurarionInSeconds = '${instance.tokenDurarionInSeconds}';
                 var inputEnableContextAwareCredentialStore = '${instance.enableContextAwareCredentialStore}';
                 var inputEnableIdentityFormatFieldsFromToken = '${instance.enableIdentityFormatFieldsFromToken}';
                 var inputIdentityFormatFieldsFromToken = '${instance.identityFormatFieldsFromToken}';
                 var inputIdentityFieldName = '${instance.identityFieldName}';
                 var allEmpty = areAllVariablesEmpty(inputAuthWebServiceId, inputIdentityFormatFieldsFromToken);
                 function areAllVariablesEmpty(...variables) {
                   return variables.every(variable => !variable.trim());
                 }
                // Check the config inputs
              if (allEmpty) {
                   document.getElementById("enableIdentityFormatFieldsFromToken").checked = true;
               }
               getIdentityFormatToken();
             }
             //Initial call to config
             enableIdentityFromToken();
        </script>
      ----
      

      Solution

      https://www.jenkins.io/doc/developer/security/csp/#inline-javascript-blocks

            Unassigned Unassigned
            basil Basil Crow
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: