You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChecksumPolicy.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package org.apache.archiva.policies;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.checksum.ChecksumAlgorithm;
  21. import org.apache.archiva.checksum.ChecksummedFile;
  22. import org.apache.archiva.checksum.UpdateStatus;
  23. import org.apache.archiva.repository.storage.StorageAsset;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.springframework.stereotype.Service;
  28. import java.io.IOException;
  29. import java.nio.file.Files;
  30. import java.nio.file.Path;
  31. import java.util.ArrayList;
  32. import java.util.Arrays;
  33. import java.util.List;
  34. import java.util.Properties;
  35. /**
  36. * ChecksumPolicy - a policy applied after the download to see if the file has been downloaded
  37. * successfully and completely (or not).
  38. *
  39. *
  40. */
  41. @Service( "postDownloadPolicy#checksum" )
  42. public class ChecksumPolicy
  43. extends AbstractPolicy implements PostDownloadPolicy
  44. {
  45. private Logger log = LoggerFactory.getLogger( ChecksumPolicy.class );
  46. private static final String ID = "checksum";
  47. /**
  48. * The IGNORE policy indicates that if the checksum policy is ignored, and
  49. * the state of, contents of, or validity of the checksum files are not
  50. * checked.
  51. */
  52. public static final ChecksumOption IGNORE = ChecksumOption.IGNORE;
  53. /**
  54. * The FAIL policy indicates that if the checksum does not match the
  55. * downloaded file, then remove the downloaded artifact, and checksum
  56. * files, and fail the transfer to the client side.
  57. */
  58. public static final ChecksumOption FAIL = ChecksumOption.FAIL;
  59. /**
  60. * The FIX policy indicates that if the checksum does not match the
  61. * downloaded file, then fix the checksum file locally, and return
  62. * to the client side the corrected checksum.
  63. */
  64. public static final ChecksumOption FIX = ChecksumOption.FIX;
  65. private List<ChecksumAlgorithm> algorithms = Arrays.asList( ChecksumAlgorithm.SHA256, ChecksumAlgorithm.SHA1, ChecksumAlgorithm.MD5 );
  66. private List<PolicyOption> options = new ArrayList<>( 3 );
  67. public ChecksumPolicy()
  68. {
  69. super();
  70. options.add( FAIL );
  71. options.add( FIX );
  72. options.add( IGNORE );
  73. }
  74. @Override
  75. public void applyPolicy( PolicyOption policySetting, Properties request, StorageAsset localFile )
  76. throws PolicyViolationException, PolicyConfigurationException
  77. {
  78. if ( "resource".equals( request.getProperty( "filetype" ) ) )
  79. {
  80. return;
  81. }
  82. if ( !options.contains( policySetting ) )
  83. {
  84. // Not a valid code.
  85. throw new PolicyConfigurationException(
  86. "Unknown checksum policy setting [" + policySetting + "], valid settings are [" + StringUtils.join(
  87. options.iterator(), "," ) + "]" );
  88. }
  89. if ( IGNORE.equals( policySetting ) )
  90. {
  91. // Ignore.
  92. log.debug( "Checksum policy set to IGNORE." );
  93. return;
  94. }
  95. if ( !localFile.exists() )
  96. {
  97. // Local File does not exist.
  98. throw new PolicyViolationException(
  99. "Checksum policy failure, local file " + localFile.getPath() + " does not exist to check." );
  100. }
  101. if ( FAIL.equals( policySetting ) && localFile.isFileBased() )
  102. {
  103. ChecksummedFile checksum = new ChecksummedFile( localFile.getFilePath() );
  104. if ( checksum.isValidChecksums( algorithms ) )
  105. {
  106. return;
  107. }
  108. for ( ChecksumAlgorithm algorithm : algorithms )
  109. {
  110. Path file = checksum.getChecksumFile( algorithm );
  111. try
  112. {
  113. Files.deleteIfExists( file );
  114. }
  115. catch ( IOException e )
  116. {
  117. log.error("Could not delete file {}", file);
  118. }
  119. }
  120. try
  121. {
  122. localFile.getStorage().removeAsset( localFile );
  123. }
  124. catch ( IOException e )
  125. {
  126. log.error("Could not delete file {}", localFile);
  127. }
  128. throw new PolicyViolationException(
  129. "Checksums do not match, policy set to FAIL, " + "deleting checksum files and local file "
  130. + localFile.getPath() + "." );
  131. }
  132. if ( FIX.equals( policySetting ) && localFile.isFileBased())
  133. {
  134. ChecksummedFile checksum = new ChecksummedFile( localFile.getFilePath() );
  135. if ( checksum.fixChecksums( algorithms ).getTotalStatus() != UpdateStatus.ERROR )
  136. {
  137. log.debug( "Checksum policy set to FIX, checksum files have been updated." );
  138. return;
  139. }
  140. else
  141. {
  142. throw new PolicyViolationException(
  143. "Checksum policy set to FIX, " + "yet unable to update checksums for local file "
  144. + localFile.getPath() + "." );
  145. }
  146. }
  147. throw new PolicyConfigurationException(
  148. "Unable to process checksum policy of [" + policySetting + "], please file a bug report." );
  149. }
  150. @Override
  151. public PolicyOption getDefaultOption()
  152. {
  153. return FIX;
  154. }
  155. @Override
  156. public String getId()
  157. {
  158. return ID;
  159. }
  160. @Override
  161. public List<PolicyOption> getOptions()
  162. {
  163. return options;
  164. }
  165. }